Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1555576
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2008-03-12 14:53:14


  1. // aha0410.cpp : 水管工
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. int a[50][50];
  5. int mark[50][50],n,m,flag=0;
  6. typedef struct _note{
  7.    int x;
  8.    int y;
  9. }note;
  10. note s[100];
  11. int top=-1;
  12. void plumber(int x,int y,int front)
  13. {
  14.    int i;
  15.    if(x==n-1 && y==m){
  16.       flag=1;
  17.       for(i=0;i<=top;i++)
  18.          printf("(%d,%d)\n",s[i].x,s[i].y);
  19.       return;
  20.    }
  21.    if(x<0 || x>n-1 || y<0 || y>m-1)
  22.       return;
  23.    if(mark[x][y]==1)
  24.       return;
  25.    mark[x][y]=1;

  26.    top++,s[top].x=x,s[top].y=y;

  27.    if(a[x][y]>=5 && a[x][y]<=6)
  28.    {
  29.       if(front==1)
  30.          plumber(x,y+1,1);
  31.       if(front==2)
  32.          plumber(x+1,y,2);
  33.       if(front==3)
  34.          plumber(x,y-1,3);
  35.       if(front==4)
  36.          plumber(x-1,y,4);
  37.    }

  38.    if(a[x][y]>=1 && a[x][y]<=4)
  39.    {
  40.       if(front==1){
  41.          plumber(x+1,y,2);
  42.          plumber(x-1,y,4);
  43.       }
  44.       if(front==2){
  45.          plumber(x,y+1,1);
  46.          plumber(x,y-1,3);
  47.       }
  48.       if(front==3){
  49.          plumber(x+1,y,2);
  50.          plumber(x-1,y,4);
  51.       }
  52.       if(front==4){
  53.          plumber(x,y+1,1);
  54.          plumber(x,y-1,3);
  55.       }
  56.    }
  57.    mark[x][y]=0;//回溯
  58.    top--;
  59.    return;
  60. }

  61. int _tmain(int argc, _TCHAR* argv[])
  62. {
  63.    int i,j,num=0;
  64.    freopen("aha0410in.txt","r",stdin);
  65.    scanf("%d %d",&n,&m);
  66.    for(i=0;i<n;i++)
  67.       for(j=0;j<m;j++)
  68.          scanf("%d",&a[i][j]);
  69.    plumber(0,0,1);
  70.    if(flag==0)
  71.       printf("Impossible......\n");
  72.    return 0;
  73. }
/* input
5 4
5 3 5 3
1 5 3 0
2 3 5 1
6 1 1 5
1 5 5 4
*/

/* output
(0,0)
(0,1)
(1,1)
(2,1)
(2,2)
(2,3)
(3,3)
(4,3)
*/

阅读(2434) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~