zoj 1002 Fire Net (DFS搜索)
2013-04-22 21:17
281 查看
1002 Fire Net
Fire NetTime Limit: 2 Seconds Memory Limit: 65536 KB
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
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 blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
View Code
Fire NetTime Limit: 2 Seconds Memory Limit: 65536 KB
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
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 blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
View Code
// File Name: Fire Net 1002.cpp // Author: sheng // Created Time: 2013年04月22日 星期一 17时07分28秒 #include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int maxn = 5; int m_ans, n; char map[maxn][maxn]; bool Build (int row, int col) { int i; for (i = row; i >= 0; i --)//检查之前的状态 { if (map[i][col] == 'O') // 表明在这一列已经存在一个碉堡了 && 中间没有墙 return false; if (map[i][col] == 'X') // 表明这一列中间有一堵墙,说明可以放个碉堡 break; } for (i = col; i >= 0; i --) //同上 { if (map[row][i] == 'O') return false; if (map[row][i] == 'X') break; } return true; } void Dfs(int s, int ans) { int row, col; if (s == n * n) { if (ans > m_ans) m_ans = ans; return; } row = s/n; col = s%n; if (map[row][col] == '.' && Build (row, col)) { map[row][col] = 'O'; Dfs (s+1, ans + 1); map[row][col] = '.'; //使用完之后还原继续递归 } Dfs (s+1, ans); } int main() { while (scanf ("%d", &n) && n) { m_ans = 0; for (int i = 0; i < n; i ++) scanf ("%s", map[i]); Dfs(0, 0); printf ("%d\n", m_ans); } return 0; }
相关文章推荐
- [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
- 搜索 Q - 素数环(dfs)
- [LeetCode] Unique Binary Search Trees II dfs 深度搜索
- UVALive 5844 dfs暴力搜索
- 图论(五)深度优先搜索DFS
- 深度遍历搜索DFS
- 【搜索】棋盘问题(DFS)
- NOJ1102黑白图像——深度搜索DFS
- POJ-1979 深度优先搜索DFS
- poj1088 滑雪 dp记忆化搜索 dfs深度优先搜索
- codeforces 132C Logo Turtle dp DFS 搜索答案
- codevs 1116四色问题 DFS搜索
- poj 2676(dfs求解数独问题,对行列和格子分别加bool数组优化搜索)
- HDOJ 题目2208唉,可爱的小朋友(搜索,dfs)
- DFS深度搜索算法实现深度探究解析-以POJ 1040为例
- [BZOJ 1052] 覆盖问题 二分+搜索(DFS)+贪心
- POJ - 1321 棋盘问题(15.10.10 搜索专题)dfs
- HDU5135 dfs搜索 枚举种数
- DFS BFS 搜索总结
- Vijos P1051 送给圣诞夜的极光(DFS搜索)