uva 639 Don't Get Rooked ( 回溯 )

來源:互聯網
上載者:User

標籤:

這道題確實是標準的回溯,果然還是早上比較清醒一些,昨天晚上想了好長一會兒都沒有想起來,早上一會的功夫就A

了,估計也有昨天晚上的協助。。。總感覺不想寫太多私人的東西在這上面,因為這個是每個人都可以無條件訪問的。。。

思路:

由於資料比較小,可以把每個元素都遍曆一遍,回溯選擇,最多4*4,還是很小的,我交的才1ms,1A。。

貼代碼:

#include<stdio.h>#include<string.h>#include<stdlib.h>int map[10][10];int visit[10][10];int row[10];//用來判斷該行是否可以放置 int col[10];//用來判斷該列是否可以放置 int max,cnt,n;void solve(int x,int y)//x,y表示將要訪問的行數和列數 {for(int i=x; i<=n; i++){for(int j=y; j<=n; j++){if(map[i][j] == 'X')//如果有牆了,就說明這一行和這一列以後可以放置了 {row[i] = 0;col[j] = 0;}else if(visit[i][j] == 0 && col[j] == 0  && row[i] == 0)//沒有訪問過+該行可以訪問+該列可以訪問 {cnt++;if(cnt > max)max = cnt;visit[i][j] = 1;row[i] = 1;col[j] = 1;if(x<n && y<n)solve(x, y+1);else if(x<n && y==n)solve(x+1, 1);else if(x == n && y < n)solve(x,y+1);else if(x == n && y == n)return ;visit[i][j] = 0;// 回溯 row[i] = 0;//回溯 col[j] = 0;//回溯 cnt--;//回溯 }}}return ;}int main(){int i,j;while(scanf("%d",&n),n!=0){memset(visit,0,sizeof(visit));memset(row,0,sizeof(row));memset(col,0,sizeof(col));memset(map,'\0',sizeof(map));getchar();for(i=1; i<=n; i++){for(j=1; j<=n; j++){scanf("%c",&map[i][j]);}getchar();}max = 0;cnt = 0;solve(1,1);printf("%d\n",max);}return 0;} 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

uva 639 Don't Get Rooked ( 回溯 )

聯繫我們

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