|
784-maze Token |
7758 |
41.30% |
2328 |
84.06% |
Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 725
Question type: Search
Sample input:
2XXXXXXXXXX X XX * XX X XXXXXXXXXXX XX XX XXXXXX_____XXXXXX XX * XX XXXXXX_____
Sample output:
XXXXXXXXXX###X###XX#######XX###X###XXXXXXXXXXX XX XX XXXXXX_____XXXXXX###XX###XX###XXXXXX_____
Analysis:
This is also a simple question for getting started with search. I have a lot of questions every day. Today's holiday is very happy. In this hot day, water and water should be lowered and cooled down.
Code 1: DFS
#include<iostream>#include<cstdio>#include<cctype>#include<cstring>using namespace std;char map[35][100];int vis[35][100], row;int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};inline bool isWall(char ch){ if(isprint(ch) && ch!=' ' && ch!='*' && ch!='-') return true; return false;}void dfs(int x,int y){ for(int i=0; i<4; ++i){ int dx=x+dir[i][0], dy=y+dir[i][1]; if(dx>=0 && dx<row && dy>=0 && dy<strlen(map[dx]) && !isWall(map[dx][dy]) && map[dx][dy]!='#' && !vis[dx][dy]){ vis[dx][dy] = true; map[dx][dy] = '#'; dfs(dx, dy); } }}int main(){#ifdef LOCAL freopen("input.txt","r",stdin);#endif int T, start_x, start_y; scanf("%d%*c", &T); while(T--){ row=0; bool isFind = false; while(gets(map[row])){ if(map[row][0]=='_') break; if(!isFind){ for(int i=0; i<strlen(map[row]); ++i){ if(map[row][i]=='*'){ start_x = row, start_y = i; isFind = true; break; } } } ++row; } memset(vis, 0, sizeof(vis)); dfs(start_x, start_y); for(int i=0; i<=row; ++i) puts(map[i]); } return 0;}
Code 2: BFS
#include<iostream>#include<cstdio>#include<cctype>#include<cstring>using namespace std;char map[35][100];int vis[35][100], row;int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};struct Node{int x,y; };Node que[10000];inline bool isWall(char ch){ if(isprint(ch) && ch!=' ' && ch!='*' && ch!='-') return true; return false;}void bfs(int x,int y){ int front=0, rear=1; que[0].x = x, que[0].y = y; while(front<rear){ Node t = que[front++]; for(int i=0; i<4; ++i){ int dx=t.x+dir[i][0], dy=t.y+dir[i][1]; if(dx>=0 && dx<row && dy>=0 && dy<strlen(map[dx]) && !isWall(map[dx][dy]) && map[dx][dy]!='#' && !vis[dx][dy]){ vis[dx][dy] = true; map[dx][dy] = '#'; Node q; q.x = dx, q.y = dy; que[rear++] = q; } } }}int main(){#ifdef LOCAL freopen("input.txt","r",stdin);#endif int T, start_x, start_y; scanf("%d%*c", &T); while(T--){ row=0; bool isFind = false; while(gets(map[row])){ if(map[row][0]=='_') break; if(!isFind){ for(int i=0; i<strlen(map[row]); ++i){ if(map[row][i]=='*'){ start_x = row, start_y = i; isFind = true; break; } } } ++row; } memset(vis, 0, sizeof(vis)); bfs(start_x, start_y); for(int i=0; i<=row; ++i) puts(map[i]); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double