zoukankan      html  css  js  c++  java
  • zoj 2110 Tempter of the Bone

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1110

    解题思路:DFS搜索

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: zoj 2110
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 typedef long long LL;
     26 const double PI=acos(-1.0);
     27 ///////////////////////////////////////////////////////////////////////////
     28 
     29 ///////////////////////////////////////////////////////////////////////////
     30 //Add Code:
     31 int n,m,t,Di,Dj;
     32 char maze[10][10];
     33 const int x[]={0,1,0,-1};
     34 const int y[]={1,0,-1,0};
     35 
     36 bool DFS(int i,int j,int k){
     37     if(i<1 || i>n || j<1 || j>m) return 0;
     38     if(i==Di && j==Dj && k==t) return 1;
     39     int ii=abs(i-Di),jj=abs(j-Dj);  //ii+jj表示从当前位置走到门的最小步数
     40     if(ii+jj>t-k || (ii+jj+t-k)&1) return 0;  //最小步数大于剩余时间或最小步数与剩余时间奇偶性不同
     41     for(int p=0;p<4;p++){
     42         if(maze[i+x[p]][j+y[p]]=='.'){
     43             maze[i+x[p]][j+y[p]]='X';
     44             if(DFS(i+x[p],j+y[p],k+1)) return 1;
     45             maze[i+x[p]][j+y[p]]='.';
     46         }
     47     }
     48     return 0;
     49 }
     50 ///////////////////////////////////////////////////////////////////////////
     51 
     52 int main(){
     53     ///////////////////////////////////////////////////////////////////////
     54     //Add code:
     55     while(scanf("%d%d%d",&n,&m,&t)!=EOF){
     56         if(!(n||m||t)) break;
     57         int Si,Sj,cnt=0;
     58         char ch;
     59         scanf("%c",&ch);
     60         for(int i=1;i<=n;i++){
     61             for(int j=1;j<=m;j++){
     62                 scanf("%c",&maze[i][j]);
     63                 if(maze[i][j]=='S'){
     64                     Si=i;
     65                     Sj=j;
     66                     maze[i][j]='X';
     67                 }
     68                 else if(maze[i][j]=='D'){
     69                     Di=i;
     70                     Dj=j;
     71                     cnt++;
     72                     maze[i][j]='.';
     73                 }
     74                 else if(maze[i][j]=='.') cnt++;
     75             }
     76             scanf("%c",&ch);
     77         }
     78         if(cnt<t) printf("NO
    ");  //可行点的个数小于时间t
     79         else{
     80             if(DFS(Si,Sj,0)) printf("YES
    ");
     81             else printf("NO
    ");
     82         }
     83     }
     84     ///////////////////////////////////////////////////////////////////////
     85     return 0;
     86 }
     87 
     88 ///////////////////////////////////////////////////////////////////////////
     89 /*
     90 Testcase:
     91 Input:
     92 4 4 5
     93 S.X.
     94 ..X.
     95 ..XD
     96 ....
     97 3 4 5
     98 S.X.
     99 ..X.
    100 ...D
    101 0 0 0
    102 Output:
    103 NO
    104 YES
    105 */
    106 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    Array.prototype.slice.call(arguments)
    change,propertychange,input事件小议
    H5项目常见问题汇总及解决方案
    director.js:客户端的路由---简明中文教程
    Web APP开发技巧总结
    Flex 布局教程:语法篇
    解决iPhone中overflow:scroll;滑动速度慢或者卡的问题
    flexbox布局的兼容性
    移动前端知识总结
    使用React重构百度新闻webapp前端
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3264067.html
Copyright © 2011-2022 走看看