[ZOJ 1002] Fire Net (簡單地圖搜尋)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   for   sp   

題目連結:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002

題目大意:

給你一個n*n的地圖,地圖上的空白部分可以放棋子,也有牆,問最多能放多少棋子使得棋子兩兩不會襲擊?

棋子襲擊若且唯若處在同一行或者同一列上並且中間沒有牆的阻隔。

 

真是好久沒寫過搜尋題了。。這道題都寫了這麼久!!

直接寫dfs(x,y,now) 代表我現在站在x點,y點上,那麼就只能產生兩種狀態,放棋子或者不放棋子。

然後寫一個C(x,y)代表當前點能否放置棋子。

什麼情況下可以放置棋子呢?

因為我們是從左上方到右下角放的嘛,所以說只需要看左邊和上邊有沒有棋子了。

 

代碼:

 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 #include <cctype> 7 #include <vector> 8 #include <map> 9 #include <set>10 #include <iterator>11 #include <functional>12 #include <cmath>13 #include <numeric>14 using namespace std;15 16 typedef long long LL;17 18 int n;19 char mp[10][10];20 int maxn = 0;21 22 bool C(int x,int y){23     bool hang = true,lie = true;24     for(int i=x-1;i>=1;i--){25         if( mp[i][y]==‘@‘ ) {26             hang = false;27             break;28         }29         if( mp[i][y]==‘X‘ ){30             hang = true;31             break;32         }33     }34     for(int j=y-1;j>=1;j--){35         if( mp[x][j]==‘@‘ ) {36             lie = false;37             break;38         }39         if( mp[x][j]==‘X‘ ){40             lie = true;41             break;42         }43     }44     return hang&&lie;45 }46 47 void dfs(int x,int y,int now){48 //    printf("x=%d y=%d now=%d\n",x,y,now);49     if( x==n&&y==n ){50         if( mp[x][y]==‘.‘ && C(x,y) ) now = now+1;51         maxn = max(maxn,now);52         return;53     }54     if( mp[x][y]==‘.‘ && C(x,y) ){55         mp[x][y] = ‘@‘;56         if(y+1<=n) dfs(x,y+1,now+1);57         else if(x+1<=n) dfs(x+1,1,now+1);58         mp[x][y] = ‘.‘;59     }60     if(y+1<=n) dfs(x,y+1,now);61     else if(x+1<=n) dfs(x+1,1,now);62 }63 64 int main(){65 //    freopen("output.txt","w",stdout);66     while(scanf("%d",&n)!=EOF&&n){67         if( n==0 ) break;68         maxn = 0;69         for(int i=1;i<=n;i++){70             scanf("%s",mp[i]+1);71         }72         dfs(1,1,0);73         printf("%d\n",maxn);74     }75     return 0;76 }

 

[ZOJ 1002] Fire Net (簡單地圖搜尋)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.