Maze Exploration |
A maze of rectangular rooms is represented on a two dimensional grid as illustrated in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same character which can be any printable character different than `*', `_' and space. In figure 1 this character is `X'. All the other points of the grid are marked by spaces.
XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX X X X X X X X###X###X###X X X X X X X X###########X X X X X X X X X X###X###X###X X X XXXXXX XXX XXXXXXXXXX XXXXXX#XXX#XXXXXXXXXX X X X X X X X X###X###X###X###X X X * X X X###############X X X X X X X X X###X###X###X###X XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX
Figure 1. Mazes of rectangular rooms
All rooms of the maze are equal sized with all walls 3 points wide and 1 point thick as illustrated in figure 2. In addition, a wall is shared on its full length by the separated rooms. The rooms can communicate through doors, which are positioned in the middle of walls. There are no outdoor doors.
door | XX XX X . X measured from within the room door - ...-- walls are 3 points wide X . X__ XXXXX | |___ walls are one point thick
Your problem is to paint all rooms of a maze which can be visited starting from a given room, called the `start room' which is marked by a star (`*') positioned in the middle of the room. A room can be visited from another room if there is a door on the wall which separates the rooms. By convention, a room is painted if its entire surface, including the doors, is marked by the character `#' as shown in figure 1b.
The lines of the input file can be of different length. The text which represents a maze is terminated by a separation line full of underscores (`_'). There are at most 30 lines and at most 80 characters in a line for each maze
The program reads the mazes from the input file, paints them and writes the painted mazes on the standard output.
2 XXXXXXXXX X X X X * X X X X XXXXXXXXX X X X X X X XXXXX _____ XXXXX X X X * X X X XXXXX _____
XXXXXXXXX X###X###X X#######X X###X###X XXXXXXXXX X X X X X X XXXXX _____ XXXXX X###X X###X X###X XXXXX _____ 感觉很简单的题,写了个bfs wrong到死,dfs直接a了,
#include <stdio.h> #include <string.h> char s[40][100],line,move[4][2]={1,0,-1,0,0,1,0,-1};//擦擦擦定义错了nnd,数字定义到字符竟然还能对,没发现服了,细节my god debug 2小时终于发现了 void dfs(int x,int y) {int i,X,Y,l=strlen(s[x]); for (i=0;i<4;i++) {X=x+move[i][0]; Y=y+move[i][1]; if ((X>=0)&&(X<line)&&(Y>=0)&&(Y<l)&&(s[X][Y]==' ')) {s[X][Y]='#'; dfs(X,Y); } } } int main() {int i,j,t,n,xx,yy; scanf("%d",&t); getchar(); while (t--) {line=0; while (gets(s[line])) if (s[line][0]=='_') break; else ++line; for (i=0;i<line;i++) for (j=0;j<strlen(s[i]);j++) if (s[i][j]=='*') {s[i][j]='#'; xx=i; yy=j; break;} dfs(xx,yy); for (i=0;i<=line;i++) puts(s[i]); } return 0; } 不知道哪错了bfs呜呜#include <stdio.h> #include <string.h> struct node {int x,y; }q[4000]; char s[40][100],top,tail,line,move[4][2]={1,0,-1,0,0,1,0,-1}; int bfs() {int i,X,Y,l; if (top>tail) return 0; //貌似是这里出问题,但是不知道为什么错...... l=strlen(s[q[top].x]); for (i=0;i<4;i++) {X=q[top].x+move[i][0]; Y=q[top].y+move[i][1]; if ((X>=0)&&(X<line)&&(Y>=0)&&(Y<l)&&(s[X][Y]==' ')) {s[X][Y]='#'; ++tail; q[tail].x=X; q[tail].y=Y; } } ++top; if (top<=tail) bfs(); } int main() {int i,j,t,n; scanf("%d",&t); getchar(); while (t--) {line=0; while (gets(s[line])) if (s[line][0]=='_') break; else ++line; for (i=0;i<line;i++) for (j=0;j<strlen(s[i]);j++) if (s[i][j]=='*') {s[i][j]='#'; q[1].x=i;q[1].y=j; break;} top=1; tail=1; bfs(); for (i=0;i<=line;i++) puts(s[i]); } return 0; }//BFS版本过了#include <stdio.h> #include <string.h> struct node {int x,y; }q[20000]; char s[40][100],top,tail,line,move[4][2]={1,0,-1,0,0,1,0,-1}; int bfs() {int i,X,Y,l; if (q[top].x==0) return 0; //改了这个 l=strlen(s[q[top].x]); for (i=0;i<4;i++) {X=q[top].x+move[i][0]; Y=q[top].y+move[i][1]; if ((X>=0)&&(X<line)&&(Y>=0)&&(Y<l)&&(s[X][Y]==' ')) {s[X][Y]='#'; ++tail; q[tail].x=X; q[tail].y=Y; q[tail+1].x=0; } } ++top; bfs(); } int main() {int i,j,t,n; scanf("%d",&t); getchar(); while (t--) {line=0; while (gets(s[line])) if (s[line][0]=='_') break; else ++line; for (i=0;i<line;i++) for (j=0;j<strlen(s[i]);j++) if (s[i][j]=='*') {s[i][j]='#'; q[1].x=i;q[1].y=j;q[2].x=0; break;} top=1; tail=1; bfs(); for (i=0;i<=line;i++) puts(s[i]); } return 0; }