hdu 5335 Walk Out (2015 Multi-University Training Contest 4),hdu5335

來源:互聯網
上載者:User

hdu 5335 Walk Out (2015 Multi-University Training Contest 4),hdu5335
Walk Out                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                               Total Submission(s): 194    Accepted Submission(s): 32


Problem DescriptionIn 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. 
InputThe 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. 
OutputFor 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

22 211113 3001111101
 
Sample Output
111101
 

題目大意:       從(1,1)到(n,m),路徑形成的位元最大。
解題思路:      BFS+貪心,先是bfs找到離目標最近的距離,後用貪心讓最前面的儘可能為0。策略就是每往前走一步,判斷是否可以是0,方法就是找它前面離它最近的可以取0的那一位,判斷是否可以從那個位置走到當前的位置。
代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <algorithm>using namespace std;const int maxn=1000+100;char s[maxn][maxn];bool vis[maxn][maxn];int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};struct node{    int x;    int y;};int ans;int n,m;queue<node> q;vector<node> son[maxn*2];void BFS1(){    memset(vis,false,sizeof(vis));    node p;    p.x=1;    p.y=1;    ans=0;    vis[1][1]=true;    while(!q.empty())        q.pop();    if(s[1][1]=='0')    {        ans=2;        q.push(p);    }    while(!q.empty())    {        node p2;        p=q.front();        q.pop();        if(p.x==n&&p.y==m)        {            if(s[n][m]=='0')            {                ans=n+m;                vis[n][m]=true;            }            break;        }        for(int i=0; i<4; i++)        {            p2.x=p.x+dir[i][0];            p2.y=p.y+dir[i][1];            if(p2.x>0&&p2.x<=n&&p2.y>0&&p2.y<=m&&!vis[p2.x][p2.y]&&s[p2.x][p2.y]=='0')            {                vis[p2.x][p2.y]=true;                if(p2.x+p2.y>ans)                {                    ans=p2.x+p2.y;                }                q.push(p2);            }        }    }}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);        }        BFS1();        if(ans==n+m)            printf("0\n");        else        {            for(int i=0; i<=n+m; i++)                son[i].clear();            int cur=0;//記錄前一位0出現的位置            if(ans==0)//起點為1                ans=1;            else            {                for(int i=1; i<=n; i++)//找到所有的離(n,m)最近的點。                {                    int j=ans-i;                    if(j>=1&&j<=m&&vis[i][j]&&s[i][j]=='0')                    {                        node v;                        v.x=i;                        v.y=j;                        son[ans].push_back(v);                    }                }                cur=ans;            }            for(int i=ans+1; i<=n+m; i++)//枚舉每一步            {                if(cur==0)//前面不存在0                {                    for(int j=1; j<=n; j++)                    {                        int k=i-j;                        node v;                        if(k>=1&&k<=m&&s[j][k]=='0')                        {                            v.x=j;                            v.y=k;                            son[i].push_back(v);                            cur=i;                        }                    }                }                else                {                    for(int j=1; j<=n; j++)                    {                        int k=i-j;                        node v;                        if(k>=1&&k<=m&&s[j][k]=='0')                        {                            for(int l=0; l<son[cur].size(); l++)                            {                                v=son[cur][l];                                if(v.x<=j&&v.y<=k&&i-cur>=j+k-v.x-v.y)//判斷前面的0是否可達                                {                                    node w;                                    w.x=j;                                    w.y=k;                                    son[i].push_back(w);                                    break;                                }                            }                        }                    }                    if(son[i].size()>0)                    cur=i;                }            }            for(int i=ans+1; i<=n+m; i++)            {                if(son[i].size()>0)                    printf("0");                else                    printf("1");            }            printf("\n");        }    }    return 0;}



     

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.