C++でプログラム書いた
#include<iostream> #include<vector> #include<string> #include<map> using namespace std; struct cell_t { int wall[4]; bool memo; cell_t(){wall[0]=wall[1]=wall[2]=wall[3]=0;}; }; int turnR(int dir) { return (dir+1)%4; } int turnL(int dir) { return (4+dir-1)%4; } vector<string> split(const string &s) { vector<string> ret; int sep_pos = s.find_first_of(" "); ret.push_back(s.substr(0,sep_pos)); ret.push_back(s.substr(sep_pos)); return ret; } void print(vector<vector<cell_t> > m) { for(int y=0;y<m[0].size();++y) { for(int x=0;x<m.size();++x) { if(m[x][y].memo)printf("!,"); else printf("#,"); } puts(""); } } struct mem6 { int dir,down,up,left,right,w,h; }; mem6 search_maze(string s,mem6 m) { int now_dir = m.dir; int up=0,down=0,left=0,right=0; int h = 0,w = 0; mem6 ret; for(int i=1;i<s.size()-1;++i) { if(s[i]=='R')now_dir=turnR(now_dir); else if(s[i]=='L')now_dir=turnL(now_dir); else { switch(now_dir) { case 0: { --h;if(h<0)up=max(up,abs(h)); break; } case 2: { ++h; down = max(down,h); break; } case 1: { ++w; right = max(right,w); break; } case 3: { --w;if(w<0)left=max(left,abs(w)); break; } } } } ret.dir = now_dir; ret.down = down; ret.up = up; ret.left = left; ret.right = right; ret.h = h; ret.w = w; return ret; } pair<int,pair<int,int> > simulate(vector<vector<cell_t> > &matrix,pair<int,pair<int,int> > start,string ope) { pair<int,int> pos = start.second; int now_dir = start.first; matrix[pos.first][pos.second].memo=1; matrix[pos.first][pos.second].wall[turnL(turnL(now_dir))]=1; for(int i=1;i<ope.size()-1;++i) { if(ope[i]=='R')now_dir=turnR(now_dir); else if(ope[i]=='L')now_dir=turnL(now_dir); else { matrix[pos.first][pos.second].wall[now_dir]=1; switch(now_dir) { case 0: { --pos.second; break; } case 2: { ++pos.second; break; } case 1: { ++pos.first; break; } case 3: { --pos.first; break; } } matrix[pos.first][pos.second].wall[turnL(turnL(now_dir))]=1; } matrix[pos.first][pos.second].memo=1; } return make_pair(turnL(turnL(now_dir)),pos); } char check_type(cell_t cell) { int *wall = cell.wall; if(wall[0]==1&&wall[2]==0&&wall[3]==0&&wall[1]==0)return '1'; if(wall[0]==0&&wall[2]==1&&wall[3]==0&&wall[1]==0)return '2'; if(wall[0]==1&&wall[2]==1&&wall[3]==0&&wall[1]==0)return '3'; if(wall[0]==0&&wall[2]==0&&wall[3]==1&&wall[1]==0)return '4'; if(wall[0]==1&&wall[2]==0&&wall[3]==1&&wall[1]==0)return '5'; if(wall[0]==0&&wall[2]==1&&wall[3]==1&&wall[1]==0)return '6'; if(wall[0]==1&&wall[2]==1&&wall[3]==1&&wall[1]==0)return '7'; if(wall[0]==0&&wall[2]==0&&wall[3]==0&&wall[1]==1)return '8'; if(wall[0]==1&&wall[2]==0&&wall[3]==0&&wall[1]==1)return '9'; if(wall[0]==0&&wall[2]==1&&wall[3]==0&&wall[1]==1)return 'a'; if(wall[0]==1&&wall[2]==1&&wall[3]==0&&wall[1]==1)return 'b'; if(wall[0]==0&&wall[2]==0&&wall[3]==1&&wall[1]==1)return 'c'; if(wall[0]==1&&wall[2]==0&&wall[3]==1&&wall[1]==1)return 'd'; if(wall[0]==0&&wall[2]==1&&wall[3]==1&&wall[1]==1)return 'e'; if(wall[0]==1&&wall[2]==1&&wall[3]==1&&wall[1]==1)return 'f'; else return'!'; } void execute(vector<vector<cell_t> > matrix) { for(int y=0;y<matrix[0].size();++y) { for(int x=0;x<matrix.size();++x) { if(matrix[x][y].memo) printf("%c",check_type(matrix[x][y])); } puts(""); } } bool comp(const pair<int,int> &a,const pair<int,int> &b) { if(a.first == b.first)return a.second < b.second; if(a.first < b.first)return 1; else return 0; } int main() { int N; cin >> N; for(int i=0;i<=N;++i) { string entrance,exits; cin >> entrance; if(entrance.size()==0)continue; cin >> exits; printf("Case #%d:\n",i+1); mem6 tmp;tmp.dir = 2; mem6 res = search_maze(entrance,tmp); res.dir = turnL(turnL(res.dir)); mem6 res2 = search_maze(exits,res); int x1 = res.left+1+res.right; int y1 = res.down+1; int x2 = res2.left+1+res2.right; int y2 = res2.down+1; int width; int height; int mostleft = 0; { vector<pair<int,int> > vi; vi.push_back(make_pair(-res2.left,2)); vi.push_back(make_pair(res2.right,2)); vi.push_back(make_pair(res2.w-res.left,1)); vi.push_back(make_pair(res2.w+res.right,1)); sort(vi.begin(),vi.end(),comp); width = abs(vi[0].first)+1+abs(vi[3].first); mostleft = vi[0].second; } { vector<int> vi; vi.push_back(-res2.up); vi.push_back(res2.down); vi.push_back(res2.h-res.up); vi.push_back(res2.h+res.down); sort(vi.begin(),vi.end()); height = abs(vi[0])+1+abs(vi[3]); } vector<vector<cell_t> > matrix(width,vector<cell_t>(height));//<x,y> for(int _y = 0; _y < matrix[0].size(); ++_y) { for(int _x = 0; _x < matrix.size(); ++_x) { matrix[_x][_y].memo = 0; } } pair<int,pair<int,int> > ent; if(mostleft==1) ent = make_pair(2,make_pair(res.left,0)); else ent = make_pair(2,make_pair(res2.left+res2.w,0)); pair<int,pair<int,int> > exi = simulate(matrix,ent,entrance); simulate(matrix,exi,exits); execute(matrix); } return 0; }
Google Code Jam2008のページから辿ったpractice problemBのC++による解答。
自分でもこんなきもいの良くrmしなかったなーと思えるのが出来たので載せておく。
最近のマシンは平気で4Gとか積んでるので、20000*20000のサイズで確保してしまえばすぐ出来る。