其他分享
首页 > 其他分享> > AOJ 0558 Cheese

AOJ 0558 Cheese

作者:互联网

#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;

typedef struct node
{
    int x,y,d;
    node(int a, int b, int c){ x = a, y = b, d = c;}
}node;

int h, w, n;
char map[1000][1000];
int ans = 0;
int xs, ys;
bool visit[1000][1000];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
queue<node> que;

void bfs()
{
    for(int i = 1; i <= n; i++)
    {
        que.push(node(xs, ys, 0));
        memset(visit, false, sizeof visit);
        while(!que.empty())
        {
            node q = que.front();
            que.pop();
            int x = q.x, y = q.y, d = q.d;
            if(map[x][y] == i+'0') 
            {
                ans += d;
                xs = x, ys = y;
                while (!que.empty()) que.pop();
                break;
            }
            for(int j = 0; j < 4; j++)
            {
                int nx = x+dir[j][0], ny = y+dir[j][1];
                if(nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx][ny] != 'X' && !visit[nx][ny]) 
                {
                    que.push(node(nx, ny, d+1));
                    visit[nx][ny] = true;
                }
            }

        }
    }
}


int main()
{
    cin >> h >> w >> n;
    for(int i = 0; i < h; i++) 
        for(int j = 0; j < w; j++) 
        {
            cin >> map[i][j];
            if(map[i][j] == 'S') xs = i, ys = j;
        }
    bfs();
    cout << ans << endl;
    system("pause");
    return 0;
}

标签:node,Cheese,int,AOJ,nx,ny,0558,&&,include
来源: https://www.cnblogs.com/znk97/p/13992921.html