社区讨论

58求助大佬

P1825[USACO11OPEN] Corn Maze S参与者 3已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lo8dxsu6
此快照首次捕获于
2023/10/27 17:02
2 年前
此快照最后确认于
2023/10/27 17:02
2 年前
查看原帖
C
//1825广搜 
#include<iostream>
#include<queue>
using namespace std;
struct s{
	int x,y,t;
};
queue<s>node;
int n,m,px,py,nx,ny,k,ex,ey;
char a[310][310];
bool bo[310][310];
int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
void chuan(int &xx,int &yy,int k)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]==a[xx][yy]&&(i!=xx||j!=yy)){
            	xx=i;
                yy=j;
                bo[xx][yy]=1;
                return;
			}
                
            }
        }
    }
    
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
			if(a[i][j]=='@')
			{
			px=i;py=j;
			}
			if(a[i][j]=='=')
			{
			ex=i;ey=j;
			}
			if(a[i][j]=='#')bo[i][j]=1;	
		}
	}
	node.push(s{px,py,0});

	while(!node.empty())
    {
        s f=node.front();
        node.pop();
        if(f.x==ex&&f.y==ey)
        {
            cout<<f.t;
            return 0;
        }
        if(a[f.x][f.y]>='A'&&a[f.x][f.y]<='Z')
        {
        
        chuan(f.x,f.y,f.t);bo[f.x][f.y]=1;
        }
        for(int i=0;i<4;i++)
        {
            nx=f.x+dx[i];
            ny=f.y+dy[i];
            if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!='#'&&!bo[nx][ny])
            {
                bo[nx][ny]=true;
                node.push(s{nx,ny,f.t+1});
            }
        
		}
        
    }
		return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...