D. Block Tower (CF 327D 搜索)
2015-03-09 20:40
483 查看
D. Block Tower
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with n rows
and m columns (it contains n × m cells).
The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly one tower of two following types:
Blue towers. Each has population limit equal to 100.
Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue
Tower. Two cells are neighbours is they share a side.
Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if needed,
see the second example for such a case).
Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population
limit is as large as possible.
He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 500).
Each of the next n lines contains m characters,
describing the grid. The j-th character in the i-th
line is '.' if you're allowed to build at the cell with coordinates (i, j) a
tower (empty cell) or '#' if there is a big hole there.
Output
Print an integer k in the first line (0 ≤ k ≤ 106) —
the number of operations Iahub should perform to obtain optimal result.
Each of the following k lines must contain a single operation in the following format:
«B x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) —
building a blue tower at the cell (x, y);
«R x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) —
building a red tower at the cell (x, y);
«D x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) —
destroying a tower at the cell (x, y).
If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.
Sample test(s)
input
output
input
output
题意:在‘-’处建房子,蓝色的可容纳100人,红色的可容纳200人(建红色房子时旁边必须要有蓝色房子),要求输出人口最大的方案,任一一解即可。
思路:现在空地上全部建蓝色的,找出独立块(被‘#’隔断互不联通),一个独立块只需要建一个蓝色房子那么其他的都可以摧毁再建红色房子。
代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with n rows
and m columns (it contains n × m cells).
The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly one tower of two following types:
Blue towers. Each has population limit equal to 100.
Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue
Tower. Two cells are neighbours is they share a side.
Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if needed,
see the second example for such a case).
Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population
limit is as large as possible.
He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 500).
Each of the next n lines contains m characters,
describing the grid. The j-th character in the i-th
line is '.' if you're allowed to build at the cell with coordinates (i, j) a
tower (empty cell) or '#' if there is a big hole there.
Output
Print an integer k in the first line (0 ≤ k ≤ 106) —
the number of operations Iahub should perform to obtain optimal result.
Each of the following k lines must contain a single operation in the following format:
«B x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) —
building a blue tower at the cell (x, y);
«R x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) —
building a red tower at the cell (x, y);
«D x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) —
destroying a tower at the cell (x, y).
If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.
Sample test(s)
input
2 3 ..# .#.
output
4 B 1 1 R 1 2 R 2 1 B 2 3
input
1 3 ...
output
5 B 1 1 B 1 2 R 1 3 D 1 2 R 1 2
题意:在‘-’处建房子,蓝色的可容纳100人,红色的可容纳200人(建红色房子时旁边必须要有蓝色房子),要求输出人口最大的方案,任一一解即可。
思路:现在空地上全部建蓝色的,找出独立块(被‘#’隔断互不联通),一个独立块只需要建一个蓝色房子那么其他的都可以摧毁再建红色房子。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 555 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FRL(i,a,b) for(i = a; i < b; i++) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct Node { int x,y; bool first; //记录是否是某个独立块的第一个 }; int n,m,ans; int dir[4][2]={1,0,0,1,-1,0,0,-1}; char mp[maxn][maxn]; bool vis[maxn][maxn]; queue<Node>Q; bool Isok(int x,int y) { if (x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]=='.') return true; return false; } void dfs(int x,int y) { int i; Node st; FRL(i,0,4) { int dx=x+dir[i][0]; int dy=y+dir[i][1]; if (Isok(dx,dy)&&!vis[dx][dy]) { ans+=3; //先要建蓝色,再摧毁,再建红色,所以自增3 vis[dx][dy]=true; st.x=dx,st.y=dy,st.first=false; dfs(dx,dy); Q.push(st); } } } int main() { int i,j; while (~sff(n,m)) { getchar(); FRE(i,1,n) { FRE(j,1,m) scanf("%c",&mp[i][j]); getchar(); } mem(vis,false); ans=0; Node st; FRE(i,1,n) { FRE(j,1,m) { if (!vis[i][j]&&mp[i][j]=='.') { ans++; //找到独立块的第一个,只需要建蓝色 vis[i][j]=true; st.x=i,st.y=j,st.first=true; dfs(i,j); Q.push(st); } } } pf("%d\n",ans); FRE(i,1,n) FRE(j,1,m) if (vis[i][j]) pf("B %d %d\n",i,j); while (!Q.empty()) { st=Q.front(); Q.pop(); if (!st.first) { pf("D %d %d\n",st.x,st.y); pf("R %d %d\n",st.x,st.y); } } } return 0; }
相关文章推荐
- cf 327D Block Tower
- CF 327D -Block Tower
- CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单
- CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单
- cf 768 B(二分搜索+线段树思维)@
- Codeforces 327D Block Tower【思维+Bfs】
- Codeforces 327D Block Tower【BFS+优先队列】
- CF-25D - Roads not only in Berland(并查集或者搜索)
- cf 484b 二分搜索/区间最值
- 比较,排序,搜索字符串对象( Comparing, Sorting, and Searching String Objects )CFString
- CF-25D - Roads not only in Berland(并查集或者搜索)
- CodeForces 327D Block Tower(DFS)
- cf 727A Transformation: from A to B(搜索)
- 如何在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能
- 如何在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能
- (搜索)cf 486D
- CF-13D - Two Paths(搜索)
- codeforces 327D Block Tower
- CF327D (dfs)
- CF - 711D 搜索环