题意:
象棋里的車可以吃横竖的車,题目加了一个墙,用于阻断攻击,问4x4的棋盘最多可以放多少只車,
思路:枚举每一个点,2^16次方种情况
#include<stdio.h> #include<iostream> #include<sstream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include<time.h> #include <stdlib.h> #include <algorithm> using namespace std; #define N 8 int m[N][N]; int vis[N]; int n; int final = 0; void dfs(int curLine, int curCol, int num) { if(curLine > n) { final = final < num ? num : final; return; } if(m[curLine][curCol] == 2) { if(curCol + 1 > n) dfs(curLine + 1, 1, num); else dfs(curLine, curCol + 1, num); return; } int ok = 1; for(int i = curLine - 1; i >= 1; i--) { if(m[i][curCol] == 2) break; if(m[i][curCol] == 1) { ok = 0; break; } } for(int i = curCol - 1; i >= 1&&ok; i--) { if(m[curLine][i] == 2) break; if(m[curLine][i] == 1) { ok = 0; break; } } if(ok) { m[curLine][curCol] = 1; if(curCol + 1 > n) dfs(curLine + 1, 1, num + 1); else dfs(curLine, curCol + 1, num + 1); m[curLine][curCol] = 0; } if(curCol + 1 > n) dfs(curLine + 1, 1, num); else dfs(curLine, curCol + 1, num); } int main(const int argc, char** argv) { //freopen("d:\1.txt", "r", stdin); while (cin >> n && n) { final = 0; memset(vis, 0, sizeof(vis)); memset(m, -1, sizeof(m)); char c; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { cin >> c; if(c == 'X') m[i][j] = 2; else m[i][j] = 0; } dfs(1, 1, 0); cout << final << endl; } return 0; }