uva 639 Don't Get Rooked (暴力回溯 ),639rooked

來源:互聯網
上載者:User

uva 639 Don't Get Rooked (暴力回溯 ),639rooked
                           uva 639 Don't Get Rooked



In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 44) that can also contain walls through which rooks cannot move. The goal is to place as many rooks on a board as possible so that no two can capture each other. A configuration of rooks islegal provided that no two rooks are on the same horizontal row or vertical column unless there is at least one wall separating them.


The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of rooks in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.

Your task is to write a program that, given a description of a board, calculates the maximum number of rooks that can be placed on the board in a legal configuration.

Input The input file contains one or more board descriptions, followed by a line containing the number 0 that signals the end of the file. Each board description begins with a line containing a positive integern that is the size of the board; n will be at most 4. The nextn lines each describe one row of the board, with a ` .' indicating an open space and an uppercase ` X' indicating a wall. There are no spaces in the input file.Output For each test case, output one line containing the maximum number of rooks that can be placed on the board in a legal configuration.Sample Input 
4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0
Sample Output 
51524


題目大意:求一個棋盤上最多能擺多少個車。

解題思路:回溯。


#include<stdio.h>#include<string.h>int gra[8][8], n, ans;int check(int x, int y) {for (int i = x + 1; i <= n && gra[i][y] != 0; i++) {  //上if (gra[i][y] == -1) return 0;}for (int i = x - 1; i >= 1 && gra[i][y] != 0; i--) {   //下if (gra[i][y] == -1) return 0;}for (int i = y + 1; i <= n && gra[x][i] != 0; i++) {  //右if (gra[x][i] == -1) return 0;}for (int i = y - 1; i >= 1 && gra[x][i] != 0; i--) {   //左if (gra[x][i] == -1) return 0;}return 1;}void DFS(int a) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (gra[i][j] == 1 && check(i, j)) {  //是否可以放置棋子, 是否不與其他棋子相對gra[i][j] = -1;DFS(a + 1);gra[i][j] = 1;}}}if (ans < a) ans = a;}int main() {while (scanf("%d", &n) == 1, n) {getchar();char temp;memset(gra, 0, sizeof(gra));for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {scanf("%c", &temp);if (temp == '.') gra[i][j] = 1;else gra[i][j] = 0;}getchar();}ans = 0;DFS(0);printf("%d\n", ans);}return 0;}



聯繫我們

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