2015 Multi-University Training Contest 4 (hdu 5335、5336)模拟

XYZ and Drops

Problem Description

XYZ is playing an interesting game called “drops”. It is played on a r∗c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property “size”. The waterdrop cracks when its size is larger than 4, and produces 4 small drops moving towards 4 different directions (up, down, left and right).

In every second, every small drop moves to the next cell of its direction. It is possible that multiple small drops can be at same cell, and they won’t collide. Then for each cell occupied by a waterdrop, the waterdrop’s size increases by the number of the small drops in this cell, and these small drops disappears.

You are given a game and a position (x, y), before the first second there is a waterdrop cracking at position (x, y). XYZ wants to know each waterdrop’s status after T seconds, can you help him?

1≤r≤100, 1≤c≤100, 1≤n≤100, 1≤T≤10000

Input

The first line contains four integers r, c, n and T. n stands for the numbers of waterdrops at the beginning.
Each line of the following n lines contains three integers xi, yi, sizei, meaning that the i-th waterdrop is at position (xi, yi) and its size is sizei. (1≤sizei≤4)
The next line contains two integers x, y.

It is guaranteed that all the positions in the input are distinct.

Multiple test cases (about 100 cases), please read until EOF (End Of File).

Output

n lines. Each line contains two integers Ai, Bi:
If the i-th waterdrop cracks in T seconds, Ai=0, Bi= the time when it cracked.
If the i-th waterdrop doesn’t crack in T seconds, Ai=1, Bi= its size after T seconds.

Sample Input

4 4 5 10
2 1 4
2 3 3
2 4 4
3 1 2
4 3 4
4 4

Sample Output

0 5
0 3
0 2
1 3
0 1

思路:模拟水滴的状态

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=110;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
struct node
{
    int x,y,st,t;
}a[maxn];
struct Small
{
    int x,y,d,id,dir;
    Small(){}
    Small(int _x,int _y,int _d,int _id,int _dir):x(_x),y(_y),d(_d),id(_id),dir(_dir){}
    bool operator<(const Small &a)const
    {
        return d>a.d;
    }
};
int R,C,N,T,X,Y;
int vis[maxn][maxn];
void process(int x,int y,priority_queue<Small> &q,int t)
{
    int tmp=x;
    vis[x][y]=-1;
    while(tmp>0&&vis[tmp][y]==-1)tmp--;
    q.push(Small(tmp,y,t+x-tmp,tmp==0?-1:vis[tmp][y],1));
    tmp=x;
    while(tmp<=R&&vis[tmp][y]==-1)tmp++;
    q.push(Small(tmp,y,t+tmp-x,tmp>R?-1:vis[tmp][y],2));
    tmp=y;
    while(tmp>0&&vis[x][tmp]==-1)tmp--;
    q.push(Small(x,tmp,t+y-tmp,tmp==0?-1:vis[x][tmp],3));
    tmp=y;
    while(tmp<=C&&vis[x][tmp]==-1)tmp++;
    q.push(Small(x,tmp,t+tmp-y,tmp>C?-1:vis[x][tmp],4));
}
void solve()
{
    priority_queue<Small> q;
    process(X,Y,q,0);

    while(!q.empty())
    {
        Small u=q.top();q.pop();
        if(u.id==-1)continue;
        if(u.d>T)continue;
        if(a[u.id].st<=4&&vis[u.x][u.y]!=-1)
        {
            a[u.id].t=u.d;
            a[u.id].st++;
            if(a[u.id].st>4)process(a[u.id].x,a[u.id].y,q,u.d);
        }
        else if(a[u.id].st>4&&a[u.id].t<u.d&&vis[u.x][u.y]==-1)
        {
            int x=u.x,y=u.y;
            int tmp,t=u.d;
            if(u.dir==1)
            {
                tmp=x;
                while(tmp>0&&vis[tmp][y]==-1)tmp--;
                if(tmp!=0)q.push(Small(tmp,y,t+x-tmp,vis[tmp][y],1));
            }
            else if(u.dir==2)
            {
                tmp=x;
                while(tmp<=R&&vis[tmp][y]==-1)tmp++;
                if(tmp<=R)q.push(Small(tmp,y,t+tmp-x,vis[tmp][y],2));
            }
            else if(u.dir==3)
            {
                tmp=y;
                while(tmp>0&&vis[x][tmp]==-1)tmp--;
                if(tmp>0)q.push(Small(x,tmp,t+y-tmp,vis[x][tmp],3));
            }
            else
            {
                tmp=y;
                while(tmp<=C&&vis[x][tmp]==-1)tmp++;
                if(tmp<=C)q.push(Small(x,tmp,t+tmp-y,vis[x][tmp],4));
            }
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(a[i].st>4)printf("0 %d\n",a[i].t);
        else printf("1 %d\n",a[i].st);
    }
}
int main()
{
    while(scanf("%d%d%d%d",&R,&C,&N,&T)!=EOF)
    {
        memset(vis,-1,sizeof(vis));
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].st);
            vis[a[i].x][a[i].y]=i;
        }
        scanf("%d%d",&X,&Y);
        solve();
    }
    return 0;
}

Walk Out

Problem Description

In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it.

An explorer gets lost in this grid. His position now is (1,1), and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he’ll write down the number on position (1,1). Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he’s on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.

Input

The first line of the input is a single integer T (T=10), indicating the number of testcases.

For each testcase, the first line contains two integers n and m (1≤n,m≤1000). The i-th line of the next n lines contains one 01 string of length m, which represents i-th row of the maze.

Output

For each testcase, print the answer in binary system. Please eliminate all the preceding 0 unless the answer itself is 0 (in this case, print 0 instead).

Sample Input

2
2 2
11
11
3 3
001
111
101

Sample Output

111
101
思路:首先dfs找出从开头是0的联通快,然后逐层递推
参考自:这里写链接内容

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int maxn=1010;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
int N,M;
char s[maxn][maxn];
bool vis[maxn][maxn],dp[maxn][maxn];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int pos;
void dfs(int x,int y)
{
    if(vis[x][y])return ;
    vis[x][y]=1;
    if(s[x][y]=='1')return ;
    dp[x][y]=1;
    if(x+y>pos)pos=x+y;
    for(int i=0;i<4;i++)
    {
        int tx=x+dx[i],ty=y+dy[i];
        if(tx<=0||tx>N||ty<=0||ty>M)continue;
        dfs(tx,ty);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++)scanf("%s",s[i]+1);
        memset(vis,0,sizeof(vis));
        memset(dp,0,sizeof(dp));
        pos=1;
        dfs(1,1);
        if(pos==N+M)
        {
            printf("0\n");
            continue;
        }
        if(pos==1)
        {
            pos=2;
            dp[1][1]=1;
            printf("1");
        }
        for(int i=pos;i<N+M;i++)
        {
            int flag=1;
            for(int j=max(1,i-M);j<=min(N,i-1);j++)
            {
                if(dp[j][i-j])
                {
                    int x=(j<N?s[j+1][i-j]-'0':1);
                    int y=(i-j<M?s[j][i-j+1]-'0':1);
                    flag=min(flag,min(x,y));
                }
            }
            for(int j=max(1,i-M);j<=min(N,i-1);j++)
            {
                if(dp[j][i-j])
                {
                    int x=(j<N?s[j+1][i-j]-'0':1);
                    int y=(i-j<M?s[j][i-j+1]-'0':1);
                    if(x==flag)dp[j+1][i-j]=1;
                    if(y==flag)dp[j][i-j+1]=1;
                }
            }
            printf("%d",flag);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值