painter
Problem Description
Mr. Hdu is an painter, as we all know, painters need ideas to innovate , one day, he got stuck in rut and the ideas dry up, he took out a drawing board and began to draw casually. Imagine the board is
a rectangle, consists of several square grids. He drew diagonally, so there are two kinds of draws, one is like ‘\’ , the other is like ‘/’. In each draw he choose arbitrary number of grids to draw. He always drew the first kind in red color, and drew the
other kind in blue color, when a grid is drew by both red and blue, it becomes green. A grid will never be drew by the same color more than one time. Now give you the ultimate state of the board, can you calculate the minimum time of draws to reach this state.
Input
The first line is an integer T describe the number of test cases.
Each test case begins with an integer number n describe the number of rows of the drawing board.
Then n lines of string consist of ‘R’ ‘B’ ‘G’ and ‘.’ of the same length. ‘.’ means the grid has not been drawn.
1<=n<=50
The number of column of the rectangle is also less than 50.
Output
Output an integer as described in the problem description.
Each test case begins with an integer number n describe the number of rows of the drawing board.
Then n lines of string consist of ‘R’ ‘B’ ‘G’ and ‘.’ of the same length. ‘.’ means the grid has not been drawn.
1<=n<=50
The number of column of the rectangle is also less than 50.
Output
Output an integer as described in the problem description.
Output
Output an integer as described in the problem description.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319
大概题意:
输入一个长为n宽度自定的矩形表格,在每个单位表格中染色,主对角线方向染红色,副对角线方向染蓝色,相同颜色不能进行重复染色,如果一个单位表格中既有红色又有蓝色,则显示为绿色,给出染色结果,求最小的染色次数。
题目分析:
模拟题,根据结果倒推答案。怎样求最小的染色次数呢?每一次可以在对角线上染任意数量的表格,每次还原到所能还原的最长对角线即为最短次数。
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
string s[55];
int n;
int simulation(){
int tot = 0;
int m = s[0].size();
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(s[i][j] == 'R'){
++tot;
for(int k = 1; k + i < n && k + j < m; ++k){ //红色还原主对角线;
if(s[k + i][k + j] == 'R')
s[k + i][k + j] = '.';
else if(s[k + i][k + j] == 'G')
s[k + i][k + j] = 'B';
else
break;
}
}
else if(s[i][j] == 'B'){
++tot;
for(int k = 1; k + i < n && j - k >= 0; ++k){ //蓝色还原副对角线;
if(s[k + i][j - k] == 'B')
s[k + i][j - k] = '.';
else if(s[k + i][j - k] == 'G')
s[k + i][j - k] = 'R';
else //到达所能还原的最长长度,结束;
break;
}
}
else if(s[i][j] == 'G'){
tot += 2;
for(int k = 1; k + i < n && k + j < m; ++k){ //绿色相当于染色两次,一次红色,一次蓝色;
if(s[k + i][k + j] == 'R')
s[k + i][k + j] = '.';
else if(s[k + i][k + j] == 'G')
s[k + i][k + j] = 'B';
else
break;
}
for(int k = 1; k + i < n && j - k >= 0; ++k){
if(s[k + i][j - k] == 'B')
s[k + i][j - k] = '.';
else if(s[k + i][j - k] == 'G')
s[k + i][j - k] = 'R';
else
break;
}
}
}
}
return tot;
}
int main()
{
int T;
cin >> T;
while(T--){
cin >> n;
for(int i = 0; i < n; ++i){
cin >> s[i];
}
int ans = simulation();
cout << ans << endl;
}
}