P1825
  • 板块题目总版
  • 楼主hyb123bc
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/19 17:01
  • 上次更新2024/5/19 19:48:01
查看原帖
P1825
929066
hyb123bc楼主2024/5/19 17:01
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cctype>
#include<stack>
#include<queue>
#include<deque>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;
int n,m; 
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
char a[305][305], cnt;
struct hyb 
{
    int x,y,step;
};
struct hyb2 
{
    int x1,y1,x2,y2;
}cs[105];
int main()
{
    cin>>n>>m;
    queue<hyb>q;
    for(int i=1;i<=n;i++) 
	{
        for(int j=1;j<=m;j++) 
		{
            cin>>a[i][j];
            cnt=a[i][j];
            if(cnt>='A'&&cnt<='Z') 
			{
                if(cs[cnt].x1==0) 
				{
                    cs[cnt].x1=i;
                    cs[cnt].y1=j;
                } 
				else 
				{
                    cs[cnt].x2=i;
                    cs[cnt].y2=j;
                }
            }
            if(cnt=='@') 
			{
                hyb tp={i,j,0};
                q.push(tp);
                a[i][j]='#';
            }
        }
    }
    while(q.empty()==false) 
	{
        hyb tp=q.front();        
		q.pop();
        for(int i=0;i<4;i++) 
		{
            int xx=tp.y+dx[i];
            int yy=tp.y+dy[i];
            if(xx<1||xx>n||yy<1||yy>m) 
			{
				continue;
			}
            if(a[xx][yy]=='#') 
			{
				continue;
			}
            if(a[xx][yy]=='.') 
			{
                hyb t={xx,yy,tp.step+1};
                q.push(t);
                a[xx][yy]='#';
                continue;
            }
            if(a[xx][yy]=='=') 
			{
                cout<<tp.step+1;
                return 0;
            }
            if(cs[a[xx][yy]].x1==xx&&cs[a[xx][yy]].y1==yy) 
			{
                hyb t={cs[a[xx][yy]].x2,cs[a[xx][yy]].y2,tp.step+1};
                q.push(t);
            } 
			else 
			{
                hyb t={cs[a[xx][yy]].x1,cs[a[xx][yy]].y1,tp.step+1};
                q.push(t);
            }
        }
    }
    return 0;
}

代码求调

2024/5/19 17:01
加载中...