hdoj 1045 Fire Net 【DFS】

來源:互聯網
上載者:User

標籤:dfs   好題   

題意:如果兩個點要放在同一行或者同一列,那麼兩個點中間要有一個牆,否則的話只能放一個點,最後問你最多能放幾個點。

看了一個星期。。

這道題的解法我還是第一次見,就是逐個逐個的來放置每個點,然後每經過一個點都判斷一次,詳情看代碼

代碼:

#include <stdio.h>#include <string.h>int ans, n;char map[10][10];int judge(int lin, int row){int i;for(i = lin-1; i >= 0; i --){  //判斷所在列if(map[i][row] == 'a') return false;if(map[i][row] == 'X') break;}for(i = row-1; i >= 0; i --){ //判斷所在行if(map[lin][i] == 'a') return false;if(map[lin][i] == 'X') break; }return true;}void dfs(int cou, int max){if(cou == n*n){ans = ans > max?ans:max;//printf("%d..\n", max);return;}else{int lin = cou/n;//這裡很精巧,仔細體會一下int row = cou%n;if(map[lin][row] == '.'&&judge(lin, row)){map[lin][row] = 'a';dfs(cou+1, max+1);map[lin][row] = '.';}dfs(cou+1, max);}}int main(){int i, j;while(scanf("%d", &n), n){memset(map, 0, sizeof(map));for(i = 0; i < n; i ++){scanf("%s", map[i]);}ans = 0;dfs(0, 0);printf("%d\n", ans);}return 0;} 

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1045

聯繫我們

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