码迷,mamicode.com
首页 > 编程语言 > 详细

【算法学习笔记】88.显式DFS SJTU OJ 2202. 梅西的过人

时间:2015-07-29 00:56:46      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>
using namespace std;

int k,n,m;
bool map[1000+5][1000+5];
bool vis[1000+5][1000+5];
int dx[4] = {0,0,-1,+1};
int dy[4] = {+1,-1,0,0};
void init(){
    cin>>n>>m;
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j){
            int t;
            scanf("%d",&t);
            map[i][j] = t;
        }    
    }
    memset(vis,0,sizeof(vis));
}

struct Point
{
    int x;
    int y;
    int done;
    Point(int a,int b){
        x = a; 
        y = b;
        done = 0;
    }
};

bool build(){
    stack<Point> s;
    Point start(1,1);
    s.push(start);
    while(!s.empty()){
        Point cur = s.top();
        s.pop();
        vis[cur.x][cur.y] = true;
        for (int i = 0; i < 4; ++i)
        {
            int new_x = cur.x + dx[i];
            int new_y = cur.y + dy[i];
            if(new_x>=1 and new_x<=n and new_y>=1 and new_y<=m){
                if(!vis[new_x][new_y]){
                    if(map[new_x][new_y]==false or cur.done == 0){
                        Point next(new_x,new_y);
                        if(map[new_x][new_y])
                            next.done = 1;
                        s.push(next);
                        vis[next.x][next.y] = true;
                        if(new_x==n and new_y==m)
                            return true;
                    }
                }
            }
        }
    }
    return false;
}

int main(int argc, char const *argv[])
{
    cin>>k;
    for (int i = 0; i < k; ++i)
    {
        init();
        cout<<build()<<endl;
    }
    return 0;
}

 

【算法学习笔记】88.显式DFS SJTU OJ 2202. 梅西的过人

标签:

原文地址:http://www.cnblogs.com/yuchenlin/p/sjtu_oj_2202.html

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