码迷,mamicode.com
首页 > 其他好文 > 详细

2015 多校赛 第四场 1009 (hdu 5335)

时间:2015-08-04 22:35:59      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
In an nm 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 (1n,m1000). 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

 

题意:给出一幅图,起点在左上角终点在左下角。沿途走过的数字连起来为最后所得的二进制数,设法令该数最小,输出之。

思路:

起点为1的情况,令二进制数长度最小,则每次只往右走或往下走,并且步数相同时走 0 优先。

起点为0的情况,则按着 0 搜到离终点最近的为 1 的点,再按上述的方式走即可。

广搜题,,但 T 到跪。看了标程打了一份。这种 bfs 方式第一次见,确实6,我还是太年轻。。

技术分享
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
char g[1005][1005];
int n,m,t,head,tail;
#define maxn 1000005
int vis[1005][1005],x[maxn],y[maxn];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
string bfs(){
    memset(vis,0,sizeof(vis));
    vis[head=0][tail=0]=1;
    x[head]=0,y[head++]=0;
    while(head!=tail){
        if(g[x[tail]][y[tail]]==0){
            for(int i=0;i<4;i++){
                int X=x[tail]+dx[i],Y=y[tail]+dy[i];
                if(X>=0&&X<n&&Y>=0&&Y<m&&!vis[X][Y]){
                    x[head]=X,y[head++]=Y;
                    vis[X][Y]=1;
                }
            }
        }
        tail++;
    }
    if(vis[n-1][m-1]&&g[n-1][m-1]==0)    return "0";
    int sum=0;
    string ans="1";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            if(vis[i][j]) sum=max(sum,i+j);
    }
    for(int k=sum;k<n+m-2;k++){
        char c=1;
        for(int i=0,j=k-i;i<n;i++,j--) if(j>=0&&j<m&&vis[i][j]){
            if(i+1<n) c=min(c,g[i+1][j]);
            if(j+1<m) c=min(c,g[i][j+1]);
        }
        ans+=c;
        for(int i=0,j=k-i;i<n;i++,j--) if(j>=0&&j<m&&vis[i][j]){
            if(i+1<n&&g[i+1][j]==c) vis[i+1][j]=1;
            if(j+1<m&&g[i][j+1]==c) vis[i][j+1]=1;
        }
    }
    return ans;
}
int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",g[i]);
        cout<<bfs()<<endl;
    }
    return 0;
}
View Code

 

2015 多校赛 第四场 1009 (hdu 5335)

标签:

原文地址:http://www.cnblogs.com/names-yc/p/4703099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!