1 条题解

  • 1
    @ 2021-12-20 18:26:00
    /*****************************************
    Note  : 
    ******************************************/
    #include <queue>
    #include <math.h>
    #include <stack>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define LL long long
    const int N = 100;
    const int INF = 0x3f3f3f3f;
    int n , m;
    int sx,sy,ex,ey;
    char a[N][N]; 
    int st[N][N];
    struct node
    {
    	int x,y,step;
    	bool operator < (const node & a)const 
    	{
    		return step > a.step;
    	}
    };
    int dx[] = {1,-1,0,0};
    int dy[] = {0,0,1,-1};
    bool flag(int x,int y)
    {
    	if(x < 0 || y < 0 || x > n || y > m)
    		return false;
    	return true;
    }
    int bfs()
    {
    	priority_queue<node> p;
    	p.push((node){sx,sy,0});
    	st[sx][sy] = 1;
    	while(!p.empty())
    	{
    		node t = p.top();
    		p.pop();
    		for(int i = 0 ; i < 4 ; i++)
    		{
    			int x = dx[i] + t.x;
    			int y = dy[i] + t.y;
    			if(!flag(x,y))	continue;
    			if(x == ex && y == ey )	 return t.step + 1;
    			if(a[x][y] == 'X') continue;
    			if(st[x][y] <= t.step)	continue;
    			st[x][y] = t.step + 1;
    			p.push((node){x,y,t.step + 1});
    			int xx = x + dx[i];
    			int yy = y + dy[i];
    			while(flag(xx,yy))
    			{
    				if(xx == ex && yy == ey)
    					return t.step + 1;
    				if(a[xx][yy] == 'X') break;
    				if(st[xx][yy] <= t.step)
    					break;
    				st[xx][yy] = t.step + 1;
    				p.push((node){xx,yy,t.step + 1});
    				xx = xx + dx[i];
    				yy = yy + dy[i];
    			}
    
    		}
    	}
    
    	return -1;
    }
    int main()
    {	
    	int t = 0;
    	while(cin >> m >> n && n + m)
    	{
    		memset(a, ' ' , sizeof(a));
    		getchar();
    		for(int i = 1 ; i <= n ; i++)
    			cin.getline(a[i]+ 1, 100);
    		n += 1;
    		m += 1;
    		printf("Board #%d:\n",++t);
    		int tt = 0;
    		while(cin >> sy >> sx >> ey >> ex && sx + sy + ex + ey )
    		{
    			memset(st,0x3f ,sizeof(st));
    			printf("Pair %d: ",++tt);
    			int ans = bfs();
    			if(ans < 0) cout << "impossible.\n";
    			else cout << ans << " segments.\n";
    		}
    		cout << endl;
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    1492
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    49
    已通过
    7
    上传者