悬赏一关注
  • 板块学术版
  • 楼主mazxht
  • 当前回复10
  • 已保存回复10
  • 发布时间2023/8/3 17:12
  • 上次更新2023/11/3 06:07:40
查看原帖
悬赏一关注
720048
mazxht楼主2023/8/3 17:12

求助大佬U322216的正解 附上我丑陋的代码

(题目数据不是很齐全,可能有错误)

#include<bits/stdc++.h>
using namespace std;
int tx[]={1,0,-1,0};
int ty[]={0,1,0,-1};
bool ok=0;
struct Point 
{
    int x, y, time;
    Point(int _x, int _y, int _time) : x(_x), y(_y), time(_time) {}
};
int escapeTime(vector<vector<char>>& map) 
{
    int n = map.size();
    int m = map[0].size();
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    queue<Point> q;
    int startX, startY;
    for (int i = 0; i < m; i++) 
        for (int j = 0; j < n; j++) 
            if (map[i][j] == 'A') 
			{
                startX = i;
                startY = j;
                //cout<<i<<" "<<j<<endl;
                break;
            }
    q.push(Point(startX, startY, 0));
    visited[startX][startY] = 1;
    while (!q.empty()) 
	{
        Point curr = q.front();
        q.pop();
        
        int x = curr.x;
        int y = curr.y;
		int time = curr.time;
        
        //cout<<x<<" "<<y<<endl;
        
	    if (map[x][y] == '@') 
	        return time;
	        
	    for(int i=0;i<4;++i)
	    {
	    	
	    	int xx=x+tx[i];
	    	int yy=y+ty[i];
	    	if(xx>=0&&xx<m&&yy>=0&&yy<n&&map[xx][yy]!='#'&&!visited[xx][yy])
	    	{
	    		q.push(Point(xx,yy,time+1));
	    		visited[xx][yy]=1;
//	    		警卫左右移动
//	    		for(int q=0;q<m;++q)
//					for(int w=0;w<n;++w)
//					{
//						if(map[q][w]=='R')
//						{
//							if(ok)
//							{
//								if (map[q][w+1]=='#'||w+1>=n)
//								{
//									ok = 0;
//									map[q][w]='.';
//									map[q][w-1]='R';
//									continue;
//								}
//								map[q][w]='.';
//								map[q][w+1]='R';
//							}
//							if(!ok)
//							{
//								if (map[q][w-1]=='#'||w-1<0)
//								{
//									ok = 1;
//									map[q][w]='.';
//									map[q][w+1]='R';
//									continue;
//								}
//								map[q][w]='.';
//								map[q][w-1]='R';
//							}
//							
//						}
//					}
//				cout<<endl;
//				for(int q=0;q<m;++q)
//				{
//					for(int w=0;w<n;++w)	
//						cout<<map[q][w];
//					cout<<endl;
//				}
//				cout<<endl;	    		
//				cout<<xx<<" "<<yy<<endl;
//	    		cout<<time+1<<endl;
			}
		}
	}
	return -1;
}
int main() 
{
	int n, m; 
	cin >> n >> m;
	vector<vector<char>> map(n, vector<char>(m));
	for (int i = 0; i < m; i++) 
	    for (int j = 0; j < n; j++) 
	        cin >> map[i][j];
	int escape = escapeTime(map);
	if (escape == -1)
	    cout << "NO" << endl;
	else 
	    cout << escape << endl;
	return 0;
}
//7 5
//##...A.
//...R.##
//##...#.
//..#..@.
//.......

2023/8/3 17:12
加载中...