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

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

文章分类

全部博文(327)

我的朋友

分类: BSD

2016-10-26 12:04:38

题目大意从矩阵1走到M的最短路径的数目,0为path, 9不可通过
只能沿x方向或者y方向行走,两个格子的距离为|x2-x1|+|y2-y1|


Code DFS
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. int p[6][2];//row 0 will not be used
  4. int a[8][8];//column 0, row 0 will not be used
  5. int book[8][8];//column 0, row 0 will not be used
  6. int num[5];//num[0] will not be used
  7. int next[4][2]={{0,1},//
  8.                 {1,0},//
  9.                {0,-1},//
  10.                {-1,0}};
  11. int N,M;
  12. int result[5];//column 0 will not be used
  13. int stopx;
  14. int stopy;
  15. int min[5];

  16. void dfs(int x,int y, int step, int l)
  17. {
  18.    int tx,ty,k;
  19.    if(x==stopx && y==stopy){
  20.       if(step<min[l]){
  21.          result[l]=1;
  22.          min[l]=step;
  23.       }else if(step==min[l]){
  24.          result[l] += 1;
  25.       }
  26.       return;
  27.    }
  28.    if(step>min[l])//剪枝.未到达但是步数已经超过目前的最小值
  29.       return;
  30.    for(k=0;k<=3;k++){
  31.       tx=x+next[k][0];
  32.       ty=y+next[k][1];
  33.       if(tx<1 || tx>N || ty<1 || ty>N)
  34.          continue;
  35.       if(a[tx][ty]!=9 && book[tx][ty]==0){
  36.          book[tx][ty]=1;
  37.          dfs(tx,ty,step+1,l);
  38.          book[tx][ty]=0;
  39.       }
  40.    }
  41. }
  42. int _tmain(int argc, _TCHAR* argv[])
  43. {
  44.    int T;
  45.    int tc;
  46.    int answer;
  47.    int i,j;
  48.    int l;
  49.    int stx,sty;
  50.    freopen("20161026i.txt","r",stdin);
  51.    freopen("20161026o.txt","w",stdout);
  52.    setbuf(stdout,NULL);
  53.    scanf("%d",&T);
  54.    for(tc=1;tc<=T;tc++){
  55.       answer=1;//对于每个case初始化结果为1
  56.       scanf("%d %d",&N,&M);//N方阵的行或者列,M最终到达的点
  57.       for(i=1;i<=N;i++){
  58.          for(j=1;j<=N;j++){
  59.             scanf("%d", &a[i][j]);
  60.             if(a[i][j]>0 && a[i][j]<=M){
  61.                p[a[i][j]][0]=i;//记录经过的点
  62.                p[a[i][j]][1]=j;
  63.             }
  64.          }
  65.       }

  66.       for(i=0;i<5;i++){
  67.          result[i]=0;
  68.          min[i]=99999999;
  69.       }
  70.       for(l=1;l<M;l++){
  71.          for(i=0;i<=N;i++){
  72.             for(j=0;j<=N;j++)
  73.                book[i][j]=0;
  74.          }
  75.          stx=p[l][0];
  76.          sty=p[l][1];
  77.          stopx=p[l+1][0];
  78.          stopy=p[l+1][1];
  79.          book[stx][sty]=1;
  80.          dfs(stx,sty,0,l);//起始点坐标, step数, 第L段
  81.       }
  82.       for(l=1;l<M;l++){
  83.          answer = answer*result[l];
  84.       }
  85.       printf("#%d %d\n",tc,answer);
  86.    }
  87.    return 0;
  88. }
Code DFS


Input
5
5 4 N为5, M为4
1 0 0 0 0
0 0 0 0 0
0 2 0 4 0
9 0 0 9 0
0 0 0 3 0
3 3
1 0 0
0 2 9
0 9 3
6 5
0 0 0 0 0 9
0 0 0 0 9 0
0 0 0 5 0 0
2 9 9 0 0 9
3 0 0 0 0 0
1 0 0 0 0 4
7 2
0 0 0 0 0 0 0
0 9 9 9 9 9 0
0 0 0 0 0 9 0
0 9 9 0 0 9 0
0 9 0 0 2 9 0
0 9 9 9 9 9 0
0 0 0 0 0 0 1
7 5
1 3 0 0 0 0 0
9 0 0 0 0 0 0
0 0 5 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 4
0 0 0 0 0 0 2

Output
#1 18
#2 0
#3 42
#4 3
#5 156508320


阅读(1340) | 评论(1) | 转发(0) |
0

上一篇:Xcode点滴

下一篇:循环链表

给主人留下些什么吧!~~

zhln2016-10-26 18:52:26

剪枝太重要了.