HNU 12894 Keys dfs

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   檔案   for   

題意:給你一個N×M的簡單圖,其中有門,牆,通道,和檔案,開啟每扇門必須要有某一把特定的鑰匙,問你最多能拿到幾個檔案

解題思路:深度優先,每一次走一個格子將它標記以後都不走,遇到門以後如果有鑰匙,將門開啟,如果沒有,將門排入佇列,搜完以後,遍曆沒有開啟的門看是否已經有鑰匙了,如果有 從門開始dfs,直到沒有可以開啟的門

解題代碼:

  1 // File Name: k.cpp  2 // Author: darkdream  3 // Created Time: 2014年08月10日 星期日 16時31分20秒  4   5 #include<vector>  6 #include<list>  7 #include<map>  8 #include<set>  9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25  26 using namespace std; 27 int t;  28 int n,m; 29 int hs[30]; 30 int nv[30]; 31 char str[200][200]; 32 struct node{ 33     int x, y ;  34 }site[30][10000]; 35 int nums[30]; 36 int xadd[] = {1,-1,0,0}; 37 int yadd[] = {0,0,-1,1}; 38 int visit[200][200]; 39 int sum = 0; 40 void dfs(int x , int y) 41 { 42     //printf("%d %d %c\n",x,y,str[x][y]); 43     for(int i = 0  ;i <= 3;i ++) 44     { 45         int tx = x + xadd[i]; 46         int ty = y + yadd[i]; 47         if(!visit[tx][ty]) 48         { 49             if(str[tx][ty] ==‘$‘)  50             { 51                 sum ++ ; 52                 visit[tx][ty] = 1; 53                 dfs(tx,ty); 54             } 55             else if(str[tx][ty]  == ‘.‘) 56             { 57                 visit[tx][ty] = 1;  58                 dfs(tx,ty); 59             }else if(str[tx][ty] <= ‘z‘ && str[tx][ty]>= ‘a‘) 60             { 61             //    printf("%c\n",str[tx][ty]); 62                 hs[str[tx][ty] - ‘a‘] = 1;  63                 visit[tx][ty] =1 ;  64                 dfs(tx,ty); 65             }else if(str[tx][ty] <= ‘Z‘ && str[tx][ty] >= ‘A‘) 66             { 67             //    printf("%d %d %c\n",tx,ty,str[tx][ty]); 68                 if(hs[str[tx][ty] - ‘A‘]) 69                 { 70                     visit[tx][ty] = 1;  71                     dfs(tx,ty); 72                 }else{ 73                     nv[str[tx][ty] - ‘A‘] ++;  74                     site[str[tx][ty] - ‘A‘][nv[str[tx][ty]-‘A‘]].x = tx;  75                     site[str[tx][ty] - ‘A‘][nv[str[tx][ty]-‘A‘]].y = ty;  76                 } 77             } 78         } 79     } 80 } 81 int main(){ 82     scanf("%d",&t); 83     while(t--) 84     { 85         memset(visit,0,sizeof(visit)); 86         sum = 0 ;  87         memset(str,0,sizeof(str)); 88         memset(hs,0,sizeof(hs)); 89         memset(nv,0,sizeof(nv)); 90         scanf("%d %d",&n,&m);  91         for(int i = 1;i <= n;i ++) 92         { 93             scanf("%s",&str[i+1][2]); 94         } 95         char temp[200] ;  96         scanf("%s",temp); 97         int ltemp = strlen(temp); 98         if(temp[0] != ‘0‘) 99             for(int i = 0 ;i < ltemp ;i ++)100             {101                 hs[temp[i] - ‘a‘] = 1; 102             }103         for(int i = 1;i <= n + 2 ; i++)104             str[i][1] = str[i][m+2] = ‘.‘;105         for(int i = 1;i <= m + 2 ;i ++ )106             str[1][i] = str[n+2][i] = ‘.‘;107         visit[1][1] = 1 ; 108         dfs(1,1);109         int ok  = 0 ; 110         do{111             ok = 0 ; 112             for(int i = 0 ;i <= 26;i ++)113             {114                 if(hs[i] && nv[i])115                 {116                     //printf("%d\n",i);117                     ok = 1;118                     visit[site[i][nv[i]].x][site[i][nv[i]].y] = 1; 119                     dfs(site[i][nv[i]].x,site[i][nv[i]].y);120                     nv[i]--;121                 }122             }123         }while(ok);124         printf("%d\n",sum);125     }126     return 0;127 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.