ZOJ 3780 Paint the Grid Again
2014-04-14 17:31
405 查看
Paint the Grid Again
Time Limit: 2 Seconds Memory Limit: 65536 KB
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).
Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color.
Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.
Please write a program to find out the way to paint the grid.
The first line contains an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is
either 'X' (black) or 'O' (white) indicates the color of the cells should be painted to, after Leo finished his painting.
Otherwise, output the solution with minimum number of painting operations. Each operation is either "R#" (paint in a row) or "C#" (paint in a column), "#" is the index (1-based) of the
row/column. Use exactly one space to separate each operation.
Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k,
the first k- 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have
the same type, the one with smaller index of row/column is the lexicographically smaller one.
Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest
一层一层的倒着找。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
char tu[550][550];
int main()
{
int T_T,n;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",tu[i]);
vector<int> ans;
int xo=0;
while(xo<n*n)
{
int base=-1,nth=-1;
bool flag=false;
for(int hang=n-1;hang>=0;hang--)
{
int i;
flag=false;
for(i=n-1;i>=0;i--)
{
if(tu[hang][i]=='O')
{
break;
}
else if(tu[hang][i]=='X')
{
flag=true;
}
}
if(i<0&&flag)
{
base=0; nth=hang;
break;
}
}
if(base==0&&nth>=0)
{
ans.push_back(nth+1);
for(int i=0;i<n;i++)
{
if(tu[nth][i]=='X')
{
xo++;
tu[nth][i]='.';
}
}
continue;
}
else
{
for(int lie=n-1;lie>=0;lie--)
{
int i;
flag=false;
for(i=n-1;i>=0;i--)
{
if(tu[i][lie]=='X')
{
break;
}
else if(tu[i][lie]=='O')
{
flag=true;
}
}
if(i<0&&flag)
{
nth=lie; base=1000;
break;
}
}
if(base==1000&&nth>=0)
{
ans.push_back(base+nth+1);
for(int i=0;i<n;i++)
{
if(tu[i][nth]=='O')
{
xo++;
tu[i][nth]='.';
}
}
continue;
}
else
{
break;
}
}
}
if(xo<n*n)
{
puts("No solution");
}
else
{
for(int sz=ans.size(),i=sz-1;i>=0;i--)
{
if(ans[i]<1000)
{
printf("R%d",ans[i]);
}
else
{
printf("C%d",ans[i]-1000);
}
if(i) putchar(32);
}
putchar(10);
}
}
return 0;
}
Time Limit: 2 Seconds Memory Limit: 65536 KB
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).
Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color.
Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.
Please write a program to find out the way to paint the grid.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:The first line contains an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is
either 'X' (black) or 'O' (white) indicates the color of the cells should be painted to, after Leo finished his painting.
Output
For each test case, output "No solution" if it is impossible to find a way to paint the grid.Otherwise, output the solution with minimum number of painting operations. Each operation is either "R#" (paint in a row) or "C#" (paint in a column), "#" is the index (1-based) of the
row/column. Use exactly one space to separate each operation.
Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k,
the first k- 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have
the same type, the one with smaller index of row/column is the lexicographically smaller one.
Sample Input
2 2 XX OX 2 XO OX
Sample Output
R2 C1 R1 No solution
Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest
一层一层的倒着找。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
char tu[550][550];
int main()
{
int T_T,n;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",tu[i]);
vector<int> ans;
int xo=0;
while(xo<n*n)
{
int base=-1,nth=-1;
bool flag=false;
for(int hang=n-1;hang>=0;hang--)
{
int i;
flag=false;
for(i=n-1;i>=0;i--)
{
if(tu[hang][i]=='O')
{
break;
}
else if(tu[hang][i]=='X')
{
flag=true;
}
}
if(i<0&&flag)
{
base=0; nth=hang;
break;
}
}
if(base==0&&nth>=0)
{
ans.push_back(nth+1);
for(int i=0;i<n;i++)
{
if(tu[nth][i]=='X')
{
xo++;
tu[nth][i]='.';
}
}
continue;
}
else
{
for(int lie=n-1;lie>=0;lie--)
{
int i;
flag=false;
for(i=n-1;i>=0;i--)
{
if(tu[i][lie]=='X')
{
break;
}
else if(tu[i][lie]=='O')
{
flag=true;
}
}
if(i<0&&flag)
{
nth=lie; base=1000;
break;
}
}
if(base==1000&&nth>=0)
{
ans.push_back(base+nth+1);
for(int i=0;i<n;i++)
{
if(tu[i][nth]=='O')
{
xo++;
tu[i][nth]='.';
}
}
continue;
}
else
{
break;
}
}
}
if(xo<n*n)
{
puts("No solution");
}
else
{
for(int sz=ans.size(),i=sz-1;i>=0;i--)
{
if(ans[i]<1000)
{
printf("R%d",ans[i]);
}
else
{
printf("C%d",ans[i]-1000);
}
if(i) putchar(32);
}
putchar(10);
}
}
return 0;
}
相关文章推荐
- [综] 训练集(train set) 验证集(validation set) 测试集(test set)
- Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE
- train
- main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])的区别
- Hailstone 在32位无符号整数中最大的跳跃次数
- AIX 安装nrpe出现 Cannot find ssl libraries错误
- 系统日志 time wait、__ratelimit、 SYN flooding含义
- 2014 Shenzhen Maker Faire见闻
- goldengate在aix上裸设备的数据库上配置extrct需要加参数RAWDEVICEOFFSET
- DataInputStream 源码分析
- raid相关的
- pthread_cond_wait 与 pthread_cond_signal 用法(惊群效应)
- f_bfree和f_bavail的区别
- ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
- GHD hair straightening iron will reward you with good hair and chic look
- 设计模式--责任链模式ChainOfResponsibility(行为型)
- 初建Rails项目
- Daily English words study
- 用postMesage实现跨域,并解决Failed to execute 'postMessage' on 'DOMWindow'
- 警告:Block的Retain Cycle的解决方法