[In Progress] HDU 5652 India and China Origins

India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.



Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can't travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.

Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
 

Input
There are multi test cases. the first line is a sinle integer  T  which represents the number of test cases.

For each test case, the first line contains two space seperated integers  N,M . next  N  lines consists of strings composed of  0,1  characters.  1  denoting that there's already a mountain at that place,  0  denoting the plateau. on  N+2  line there will be an integer  Q  denoting the number of mountains that rised up in the order of times. Next  Q  lines contain  2  space seperated integers  X,Y  denoting that at ith year a mountain rised up at location  X,Y .

T10

1N500

1M500

1QNM

0X<N

0Y<M
 

Output
Single line at which year the communication got cut off.

print -1 if these two countries still connected in the end.

Hint:



From the picture above, we can see that China and India have no communication since 4th year.
 

Sample Input
  
1 4 6 011010 000010 100001 001000 7 0 3 1 5 1 3 0 0 1 2 2 4 2 1
 

Sample Output
  
4
 

Source
 

到目前为止仍然没有AC...持续超时中... 标上In Progress先挖个坑吧= =

宽搜版本(BFS) 队列实现 

#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;

char map[550][550];
int vis[550][550];

using POINT = pair<int,int>;
bool findtrace(int n,int m)
{
    queue<POINT> work;
    memset(vis,0,sizeof(int)*550*550);
    for(int i=1;i<=m;i++)
    {
        if(map[1][i]=='0')
        {
            work.push(POINT(1,i));
            vis[1][i]=1;
        }
    }
    work.push(POINT(-1,-1));
    /// Start Executing.
    while(work.size()>1)
    {
        POINT p=work.front();
        work.pop();
        if(p.first==-1&&p.second==-1)
        {
            /// Special
            work.push(p);
            continue;
        }

        /// UP
        if(!(map[p.first-1][p.second]==0||map[p.first-1][p.second]=='1'||vis[p.first-1][p.second]==1))
        {
            work.push(POINT(p.first-1,p.second));
            vis[p.first-1][p.second]=1;
        }

        /// DOWN
        if(!(map[p.first+1][p.second]==0||map[p.first+1][p.second]=='1'||vis[p.first+1][p.second]==1))
        {
            if(p.first+1==n)
            {
                return true;
            }
            work.push(POINT(p.first+1,p.second));
            vis[p.first+1][p.second]=1;
        }

        /// LEFT
        if(!(map[p.first][p.second-1]==0||map[p.first][p.second-1]=='1'||vis[p.first][p.second-1]==1))
        {
            work.push(POINT(p.first,p.second-1));
            vis[p.first][p.second-1]=true;
        }

        /// RIGHT
        if(!(map[p.first][p.second+1]==0||map[p.first][p.second+1]=='1'||vis[p.first][p.second+1]==1))
        {
            work.push(POINT(p.first,p.second+1));
            vis[p.first][p.second+1]=true;
        }
    }
    return false;
}

int main()
{
    int t;
    scanf("%d",&t);
    for(int _t=0;_t<t;_t++)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        memset(map,0,550*550);
        for(int i=0;i<n;i++)
        {
            scanf("%s",&map[i+1][1]);
        }
        if(!findtrace(n,m))
        {
            printf("0\n");
            continue;
        }

        int year=0;
        int yy;
        scanf("%d",&year);
        bool isover=false;
        int ans=0;
        for(yy=0;yy<year;yy++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            map[x+1][y+1]=1;
            if(isover) continue;
            if(!findtrace(n,m))
            {
                ans=yy;
                isover=true;
            }
        }
        if(!isover) printf("-1\n");
        else
        {
            printf("%d\n",ans+1);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值