40WA求调
查看原帖
40WA求调
792535
_shining楼主2024/4/12 21:47
#include <iostream>//cin cout
#include <queue>//队列 
using namespace std;//命名空间
struct point{
	int x, y, step;
	bool is_juqi;//是否聚气 
};
int n;
bool flag;
queue<point> q;
int a[1505][1505];
const int dx1[] = {0, 0, 1, -1};//时空之门 
const int dy1[] = {1, -1, 0, 0};
const int dx2[] = {0, 0, 2, -2};//海洋之门 
const int dy2[] = {2, -2, 0, 0};
const int dx3[] = {1, -1, 1, -1};//天堂之门 
const int dy3[] = {1, -1, -1, 1}; 
bool check(int x, int y)//判断 
{
	if(x >= 0 && y >= 0 && x < n && y < n) return true;//越界处理 
	return false;
}
void bfs(int x, int y, int step)
{
	a[x][y] = 0;//标记
	q.push((point){x, y, 1, 0});//初始状态入队
	while(!q.empty())//队列不为空时做
	{
		point p = q.front();//取出队头
		q.pop();//出队
		if(p.is_juqi == 1){//处理聚气
			p.is_juqi = 0;//不聚气 
			p.step++;//步数+1 
			q.push(p);//入队 
			q.pop();//出队 
			continue;//跳过循环 
		}
		if((p.x == 1 && p.y == 1) || (p.x == 1 && p.y == n) || (p.x == n && p.y == 1)){//到目的地 
			cout << p.step + 1;
			flag = 1;//有输出
			return;
		}
		for(int i = 0; i < 4; i++)
		{
			p = q.front();
			p.x += dx1[i];//偏移量x
			p.y += dy1[i];//偏移量y
			p.step++;
			p.is_juqi = 0;//不聚气 
			if(check(p.x, p.y) && a[p.x][p.y] == 1)//满足条件
			{
				a[p.x][p.y] = 0;//锁定
				q.push(p);//入队
			}
		}
		for(int i = 0; i < 4; i++)
		{
			p = q.front();
			p.x += dx2[i];//偏移量x
			p.y += dy2[i];//偏移量y
			p.step++;
			p.is_juqi = 0;//不聚气
			if(check(p.x, p.y) && a[p.x][p.y] == 2)//满足条件
			{
				a[p.x][p.y] = 0;//锁定
				q.push(p);//入队
			}
		}
		for(int i = 0; i < 4; i++)
		{
			p = q.front();
			p.x += dx3[i];//偏移量x
			p.y += dy3[i];//偏移量y
			p.step++;
			p.is_juqi = 1;//聚气 
			if(check(p.x, p.y) && a[p.x][p.y] == 3)//满足条件
			{
				a[p.x][p.y] = 0;//锁定
				q.push(p);//入队
			}
		} 
	}
}
int main()//主函数
{
	cin >> n;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			char ch;
			cin >> ch;
			if(ch == '*') a[i][j] = 0;
			else a[i][j] = ch - 'A' + 1;
		}
	}
	if(n == 1 && a[1][1] != '*'){
		cout << 1;
		return 0;
	}
	if(a[n][n] == '*' || (a[1][n] == '*' && a[1][1] == '*' && a[n][1] == '*')){
		cout << "No answer";
		return 0;
	}
	bfs(n, n, 1);
	if(!flag) cout << "No answer";
	return 0;//华丽结束
}
2024/4/12 21:47
加载中...