poj 3984

dfs入门题

#include <iostream>
using namespace std;
void print(int);
int map[5][5];
struct pos{
    int x,y;
    int pre;
};
pos list[200];
int dy[4] = {1,-1,0,0};
int dx[4] = {0,0,1,-1};
int main()
{
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            cin>>map[i][j];
        }
    }
   int f=0,r=1,x,y;
   list[0].x = 0;
   list[0].y = 0;
   list[0].pre = -1;
   while(!(list[f].x==4&&list[f].y==4))
   {
       map[list[f].x][list[f].y] = 1;
       for(i=0;i<4;i++)
       {
           x = list[f].x+dx[i];
           y = list[f].y + dy[i];
           if(map[x][y]||x<0||x>4||y<0||y>4)
                continue;
           list[r].x = x;
           list[r].y = y;
           list[r].pre = f;
           r++;
       }
       f++;
   }
   print(f);
   return 0;
}
void print(int n)
{
    if(n==-1)
        return;
    else
    {
        print(list[n].pre);
        cout<<'('<<list[n].x<<", "<<list[n].y<<')'<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值