K - group fights can lose, Timo must die (the right direction and values related to bfs)

Description

For some reason you do not know, we LOL map abstracted as a matrix Timo n × m of the accumulation of the k kinds of mushrooms ready to go to the map, because Timo missed the basket, so every walk of Teemo where it will be placed under a mushroom, while two mushroom species in one place, they would explode, so once about to happen, Timo will be sent directly home, to prevent himself was killed
in qualifying before because of the chaos kinds of mushrooms Timo has been accused many times, so this time Timo Te to find information on the current value of mushroom species at various locations to do a statistical map, but because Timo action clumsy, so he can only move the lattice in vertical and horizontal four directions (the direction if there is a lattice, then
each will select the maximum travel value Triticum a right direction from four directions, if a plurality of maximum weights, he would from a plurality of among the same maximum weight and come to find out there is no selection of a suitable direction to go in order of priority and down about. If the maximum weights are gone, he will be frustrated transmission Home, this time direct output "BOOM"
(Timo will dig in his starting point a kind of mushroom go smoothly
Input

Multiple sets of input.
Input of the first line contains the map size n, m, the number of mushroom k. (1 <= n, m < = 100,1 <= k <= n * m)
The following rows each row having the number n m (and ensure that each number range [1,1e5)
The next two integers x, y represent Timo starting point
Output

If you do not come to the last place kinds of mushrooms, but the mushrooms have not finished all kinds of output "BOOM".
If the mushroom species over halfway through, the coordinates of which the output Timo "Teemo: the X-the y-."
The Sample

Input

3 3 3
1 2 3
4 5 6
7 8 9
2 2
3 3 5
1 2 3
4 5 6
7 8 9
2 2
Output

Teemo: 3 3
BOOM

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<queue>
#include<algorithm>
using namespace std;
int Map[1100][1100];
int vis[1100][1100];
int tx[] = {0,1,0,-1};
int ty[] = {1,0,-1,0};
int n,m,p;
struct node
{
    int x,y;
    int mg;//蘑菇的数量
}t,k;
void bfs(int x,int y)
{
    queue<node>q;
    t.x = x;
    t.y = y;
    t.mg = p-1;
    vis[x][y] = 1;
    q.push(t);
    while(!q.empty())
    {
        k = q.front();
        q.pop();
        if(k.mg == 0)
        {
            printf("Teemo: %d %d\n",k.x,k.y);
            return ;
        }
        int a,b,maxn=0,Max=0;
        for(int i=0;i<4;i++)
        {
            t.x = k.x+tx[i];
            t.y = k.y+ty[i];
            if(0<t.x&&t.x<=n&&0<t.y&&t.y<=m&&!vis[t.x][t.y]&&Map[t.x][t.y]>maxn)
            {
                a = t.x;
                b = t.y;
                maxn = Map[t.x][t.y];//找出前后左右四个方向上最大的
            }
            Max = max(Max,Map[t.x][t.y]);//标记当前最大的
        }
        if(maxn!=Max)//没有更大的值了
        {
            printf("BOOM\n");
            return ;
        }
        vis[a][b] = 1;
        t.x = a;
        t.y = b;
        t.mg = k.mg-1;
        q.push(t);
    }
}
int main()
{
    int sx,sy;
    while(~scanf("%d%d%d", &n, &m, &p))
    {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%d", &Map[i][j]);
            }
        }
        scanf("%d %d", &sx, &sy);
        bfs(sx, sy);
    }
    return 0;
}


Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104888323