悬赏6元调代码P1825骗人把我棕了
  • 板块学术版
  • 楼主呆呆的她啊
  • 当前回复6
  • 已保存回复6
  • 发布时间2023/11/2 19:49
  • 上次更新2023/11/3 08:05:09
查看原帖
悬赏6元调代码P1825骗人把我棕了
226167
呆呆的她啊楼主2023/11/2 19:49
#include <iostream>
#include <queue>
#include <map>
using namespace std;
struct node1 {
	int x1;
	int y1;
	int x2;
	int y2;
};
map<char, node1>ma;
int n, m,u, v,xx1,yy1;
int flag=0;
char  map1[355][355];
int tx[4] = {1, -1, 0, 0};
int ty[4] = {0, 0, 1, -1};
bool vis[355][355];
struct node {
	int x;
	int y;
	int t;
} temp;

queue<node>q;
void check(int &xx, int &yy) {
	if(ma[map1[xx][yy]].x1!=0&&ma[map1[xx][yy]].x2==0)
	{

		flag=1;
	//如果是单个字母根据讨论区说的 默认他为玉米
	}//单个字母出现时 下面是成对时
	else 
	{
		if (ma[map1[xx][yy]].x1 != xx && ma[map1[xx][yy]].y1 != yy) {
			xx = ma[map1[temp.x][temp.y]].x1;
			yy = ma[map1[temp.x][temp.y]].y1;
		} else {
			xx = ma[map1[temp.x][temp.y]].x2;
			yy = ma[map1[temp.x][temp.y]].y2;
		}
		
	}
}
void read(int n,int m)
{
	
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> map1[i][j];
			if (map1[i][j] == '@') {
				u = i;
				v = j;
				
			} else if (map1[i][j] >= 'A' && map1[i][j] <= 'Z') {
				if (ma[map1[i][j]].x1 == 0 && ma[map1[i][j]].y1 == 0) {
					ma[map1[i][j]].x1 = i;
					ma[map1[i][j]].y1 = j;
				} else {
					ma[map1[i][j]].x2 = i;
					ma[map1[i][j]].y2 = j;
				}			
			}
		}//一个简单的读入 map拿来存放传送点 不用数组 这样更快
	}
}
int main() {
	cin >> n >> m;
	read(n,m);
	vis[u][v] = 1;
	q.push((node) {		u, v, 0	});
	while (!q.empty()) {//开始广搜
		temp = q.front();
		q.pop()		;
		int xx = temp.x;
		int yy = temp.y;
		if (map1[xx][yy] >= 'A' && map1[xx][yy] <= 'Z') 
		{ 	
			check(xx,yy);	
			if(flag)
			{	flag=0;
				continue;
			}//如果是这样的话说明此时是碰到了单个字母 直接continue掉
		}	
		for (int i = 0; i < 4; i++) {
			
			xx1=xx+tx[i];
			yy1=yy+ty[i];
			if (xx1 <= 0 || yy1 <= 0 || xx1 >n || yy1 >m)
				continue;
			if(vis[xx1][yy1])
				continue;
			if (map1[xx1][yy1] == '#')
				continue;
			if (map1[xx1][yy1] == '.') {
				vis[xx1][yy1] = 1;
				q.push((node) {	xx1, yy1, temp.t+1});
			}
			if(map1[xx1][yy1]>='A'&&map1[xx1][yy1]<='Z')
			{ 
				q.push((node) {	xx1, yy1, temp.t+1});
				
			}
			if (map1[xx1][yy1] == '=') {
				cout<<temp.t+1;return 0;
			}
		}
	}
	return 0;
}
2023/11/2 19:49
加载中...