遇到了不会的东西。。。
查看原帖
遇到了不会的东西。。。
580135
docxjun楼主2022/7/4 18:58

rt

先贴个错误代码

#include<bits/stdc++.h>
using namespace std;
char a[1005][1005];
struct cp
{
	int x,y;
};
queue<cp> q;
int dx[] = {1,0,0,-1};
int dy[] = {0,1,-1,0};
cp X[1000000];
int main()
{
	int n,m;
	int sx,sy,fx,fy,xs = 0;
	cin>>n>>m;
	int to = 1;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			cin>>a[i][j];
			if(a[i][j] == 'S')
			{
				sx = i;
				sy = j;
			}
			else if(a[i][j] == 'E')
			{
				fx = i;
				fy = j;
			}
			else if(a[i][j] == 'X')
			{
				X[to] = {i,j};
				to++;
				xs++;	
			}
		}
	}
	cp start = {sx,sy};
	q.push(start);
	int ans = 0;
	bool tyh = false;
	bool lgj = false;
	while(!q.empty())
	{
		
		cp tmp = q.front();
		q.pop();
		int nx = tmp.x;
		int ny = tmp.y;
		for(int i = 0; i < 4; i++)
		{
			int tx = nx+dx[i];
			int ty = ny+dy[i];
			if(tx == fx&&ty == fy)
			{
				cout<<ans<<endl;
				return 0;
			}
			if(tx<1||tx>n||ty<1||ty>m)
			{
				continue;
			}
			if(a[tx][ty] == '0')
			{
				cp in = {tx,ty};
				q.push(in);
				ans++;
			}
			else if(a[tx][ty] == '1')
			{
				if(lgj == true)
				{
					cp in = {tx,ty};
					q.push(in);
				}
				else
				{
					continue;
				}
			}
			else if(a[tx][ty] == '2')
			{
				if(tyh == true||lgj == true)
				{
					cp in = {tx,ty};
					q.push(in);
					ans++;
				}
				else
				{
					cp in = {tx,ty};
					q.push(in);
					ans+=2;
				}
			}
			else if(a[tx][ty] == '3')
			{
				if(tyh == true||lgj == true)
				{
					cp in = {tx,ty};
					q.push(in);
					ans++;
				}
				else
				{
					cp in = {tx,ty};
					q.push(in);
					ans+=8;	
				}

			}
			else if(a[tx][ty] == '4')
			{
				tyh = true;
				cp in = {tx,ty};
				q.push(in);
				ans++;
			}
			else if(a[tx][ty] == '5')
			{
				lgj = true;
				ans += 5;
				cp in = {tx,ty};
				q.push(in);
			}
			else if(a[tx][ty] == 'X')
			{
				for(int i = 1; i<= xs; i++)
				{
					q.push(X[i]);
					ans+=2;
				}
			}
		}
	}
	cout<<"We want to live in the TouHou World forever"<<endl;
	return 0;
	
}

是这样的

按照题意的话,每走一步应该都要有一个不同的答案

比如样例1的第一行

5400000S01

从S走到左边或者右边应该答案都是1;

但是我把它俩加到了同一个变量里,导致最后答案贼大

求助应该怎么存储答案

qwq

2022/7/4 18:58
加载中...