zoukankan      html  css  js  c++  java
  • 九度 1091 棋盘游戏

    http://ac.jobdu.com/problem.php?id=1091

    牢记师姐的话~

    基本DFS,一开始越界了,搜索了好长时间,后来检查是内存越界了,不过奇怪,怎么没提示啥呢

    把方向存数组里,代码可以更短的。还有,某些人喜欢COPY别人的代码,不是好习惯,不管他了。

     1 #include <stdio.h>
    2 #define INF 0x7fffffff
    3 int cost_array[8][8];
    4 int n;
    5 int start_x,start_y,end_x,end_y;
    6 int fin_cost=INF;
    7 bool available(int x,int y)
    8 {
    9 if(!(x>=1&&x<=6&&y>=1&&y<=6))
    10 return false;
    11 if(cost_array[x][y]==0)
    12 return false;
    13 return true;
    14 }
    15 void DFS(int x,int y,int cur_cost,int cur_stat)
    16 {
    17 if(x==end_x&&y==end_y){
    18 if(cur_cost<fin_cost){
    19 fin_cost=cur_cost;
    20 }
    21 return ;
    22 }
    23 if(cur_cost>fin_cost){
    24 return ;
    25 }
    26 int temp;
    27 if(available(x,y+1)){
    28 temp=cost_array[x][y+1];
    29 cost_array[x][y+1]=0;
    30 DFS(x,y+1,cur_cost+temp*cur_stat,(temp*cur_stat)%4+1);
    31 cost_array[x][y+1]=temp;
    32 }
    33 if(available(x,y-1)){
    34 temp=cost_array[x][y-1];
    35 cost_array[x][y-1]=0;
    36 DFS(x,y-1,cur_cost+temp*cur_stat,(temp*cur_stat)%4+1);
    37 cost_array[x][y-1]=temp;
    38 }
    39 if(available(x-1,y)){
    40 temp=cost_array[x-1][y];
    41 cost_array[x-1][y]=0;
    42 DFS(x-1,y,cur_cost+temp*cur_stat,(temp*cur_stat)%4+1);
    43 cost_array[x-1][y]=temp;
    44 }
    45 if(available(x+1,y)){
    46 temp=cost_array[x+1][y];
    47 cost_array[x+1][y]=0;
    48 DFS(x+1,y,cur_cost+temp*cur_stat,(temp*cur_stat)%4+1);
    49 cost_array[x+1][y]=temp;
    50 }
    51 }
    52 int main()
    53 {
    54 while(scanf("%d",&n)!=EOF){
    55 while(n--){
    56 fin_cost=INF;
    57 int i,j;
    58 for(i=1;i<=6;i++){
    59 for(j=1;j<=6;j++){
    60 scanf("%d",&cost_array[i][j]);
    61 }
    62 }
    63 scanf("%d%d%d%d",&start_x,&start_y,&end_x,&end_y);
    64 start_x++;
    65 start_y++;
    66 end_x++;
    67 end_y++;
    68 DFS(start_x,start_y,0,1);//cur_x,cur_y,cur_cost,cor_stat;
    69 printf("%d\n",fin_cost);
    70 }
    71 }
    72 }
    73



  • 相关阅读:
    线程互斥与同步
    JSP中传递数据出现的乱码问题
    JavaWeb学习——获取类路径下的资源
    Java初始化顺序
    Socket网络编程
    算法练习--LeetCode--17. Letter Combinations of a Phone Number
    算法练习--LeetCode--29. Divide Two Integers
    XCode10 swift4.2 适配遇到的坑
    leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
    iOS 拼音 Swift K3Pinyin
  • 原文地址:https://www.cnblogs.com/yangce/p/2341659.html
Copyright © 2011-2022 走看看