Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

WA了.....好心人帮忙看看

Posted by veryhuman at 2009-02-17 23:13:57 on Problem 2157
#include <iostream>
#include <queue>

using namespace std;

struct node
{
	int x, y;
};

const int MAX = 25;
char maze[MAX][MAX];
bool gone[MAX][MAX], getKey[5], wait[5];
int nxt[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }, key[5], m, n;
node s, g, door[5];

void init ()
{
	memset ( gone, 0, sizeof(gone) );
	memset ( key, 0, sizeof(key) );
	memset ( getKey, 0, sizeof(getKey) );
	
	for ( int i = 1; i <= m; ++i )
		for ( int j = 1; j <= n; ++j )
			cin >> maze[i][j];
			
	for ( int i = 0; i <= m + 1; ++i )
		maze[i][0] = maze[i][n+1] = '/';
	for ( int j = 0; j <= n + 1; ++j )
		maze[0][j] = maze[m+1][j] = '/';
		
	for ( int i = 1; i <= m; ++i )
		for ( int j = 1; j <= n; ++j )
			if ( maze[i][j] == 'G' ){
				g.x = i;
				g.y = j;
			}
			else if ( maze[i][j] == 'S' ){
				s.x = i;
				s.y = j;
			}
			else if ( maze[i][j] >= 'a' && maze[i][j] <= 'e' ){
				getKey[maze[i][j]-'a'] = 1;
				++key[maze[i][j]-'a'];
			}
			else ;
	return;
}

int BFS()
{
	queue<node> qu;
	node temp, next;
	qu.push(s);
	gone[s.x][s.y] = 1;
	bool success = 0;
	while ( !qu.empty() ){				//可以走 
		temp = qu.front();
		qu.pop();
		if ( temp.x == g.x && temp.y == g.y ){		//GG~ 
				success = 1;
				break;
		}
		else {
			for ( int i = 0; i != 4; ++i ){					//对每个方向的那一格 
				next.x = temp.x + nxt[i][0];
				next.y = temp.y + nxt[i][1];
				if ( maze[next.x][next.y] >= 'a' && maze[next.x][next.y] <= 'e' && !gone[next.x][next.y] ){//如果是钥匙 
						qu.push(next);						//走过去 
						gone[next.x][next.y] = 1;
						--key[maze[next.x][next.y]-'a'];//捡锁匙 
						if ( !key[maze[next.x][next.y]-'a'] && wait[maze[next.x][next.y]-'a'] ){//捡完之后如果锁匙够了 且有门等待着开 
							qu.push(door[maze[next.x][next.y]-'a']);		//等阵就走那扇门 					
							gone[door[maze[next.x][next.y]-'a'].x][door[maze[next.x][next.y]-'a'].y] = 1;
						}
				}
				else if ( maze[next.x][next.y] >= 'A' && maze[next.x][next.y] <= 'E' && !gone[next.x][next.y] ){//如果是门 
					if ( getKey[maze[next.x][next.y]-'A'] ){//如果有这样的锁匙 (因为有门不一定有锁匙) 
						if ( !key[maze[next.x][next.y]-'A'] ){//如果锁匙够了! 
							qu.push(next);							//开门 
							gone[next.x][next.y] = 1;
						}
						else {												//否则锁匙不够 
							door[maze[next.x][next.y]-'A'] = temp;
							wait[maze[next.x][next.y]-'A'] = 1;			//等着开 
						}
					}
					else gone[next.x][next.y] = 1; //不能走的门当走过了 
				}
				else if ( maze[next.x][next.y] != '/' && maze[next.x][next.y] != 'X' && !gone[next.x][next.y] ){//正常移动 
					qu.push(next);
					gone[next.x][next.y] = 1;
				}
				else ;
			}
		}
	}
	return success;
}			

int main()
{
	while ( cin >> m >> n && m && n ){
		init();
		if ( BFS() ) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator