这题越界了好多次 ,crash。。。神马的。
还是没读懂题啊 !!!我木有看INPUT OUTPUT的描述
Input
One representation of the image will be given to your program in the input.
Output
Your program has to write other representation of the image to the output.
啊哭。
应该是两种模式的相互转换。
然后就很随意了。直接模拟BFS过程即可。
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <math.h> #include <queue> using namespace std; const int MAX = 150; bool map[MAX][MAX],used[MAX][MAX]; int dir[4][2] = {1,0,0,1,-1,0,0,-1}; char d[] = "RTLB"; int sx,sy,c,n; char output[MAX*500]; int out[MAX][2]; queue<int> q; bool check(int x,int y) { return x > 0 && x <= 10 && y > 0 && y <= 10 ; } int ind(char ch) { for(int i=0; i<4; i++) if( ch == d[i] ) return i; return 1; } void BFS() { while( !q.empty() ) q.pop(); q.push(sx); q.push(sy); while( !q.empty() ) { int x = q.front(); q.pop(); int y = q.front(); q.pop(); for(int i=0; i<4; i++) { int a = x + dir[i][0]; int b = y + dir[i][1]; if( check(a,b) && map[a][b] && !used[a][b] ) { used[a][b] = true; output[c++] = d[i]; q.push(a); q.push(b); } } output[c++] = ','; } } void solve1() { int x,y; sx = -1,sy = -1; c = 0; memset(map,false,sizeof(map)); memset(used,false,sizeof(used)); while( n-- ) { scanf("%d%d",&x,&y); if( sx == -1 ) { sx = x; sy = y; } map[x][y] = true; } printf("%d %d/n",sx,sy); used[sx][sy] = true; BFS(); output[c-1] = '.'; for(int i=0; i<c; i++) { printf("%c",output[i]); if( output[i] == ',' ) printf("/n"); } printf("/n"); } void solve2() { c = 0; char str[500]; sx = n; scanf("%d",&sy); memset(map,false,sizeof(map)); map[sx][sy] = 1; q.push(sx); q.push(sy); out[c][0] = sx; out[c++][1] = sy; while( ~scanf("%s",str) ) { int x = q.front(); q.pop(); int y = q.front(); q.pop(); int len = strlen(str); for(int i=0; i<len; i++) if( isalpha(str[i]) ) { int p = ind(str[i]); int a = x + dir[p][0]; int b = y + dir[p][1]; q.push(a); q.push(b); map[a][b] = 1; out[c][0] = a; out[c++][1] = b; } } printf("%d/n",c); for(int i=1; i<=10; i++) for(int k=1; k<=10; k++) if( map[i][k] ) printf("%d %d/n",i,k); } int main() { char ch; scanf("%d",&n); ch = getchar(); if( ch != ' ' ) solve1(); else solve2(); return 0; }