zoj 3780 Paint the Grid Again
来源:互联网 发布:淘宝客成交不计入权重 编辑:程序博客网 时间:2024/06/06 03:09
可以拓扑排序,也可以贪心模拟。
由于行与列必有1交点,如果有某行可删除,则任一列肯定无法删除。
每次先判断是否全是‘-’,如果是则完成,输出答案。
每次找到字典序最大的要删除的某行或某列,删除。
如果没找到,则表示No solution.
#include<iostream>#include<cstdio>#include<string.h>using namespace std;const int N=1502;int tt,n;char mp[N][N];bool col[N],row[N];bool check(){//是否全部完成for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(mp[i][j]!='-') return 0;return 1;}char ch[2*N];int id[2*N];//字典序 C<R 1<2int find(int cnt){//行Xfor(int i=n;i>=1;i--){if(row[i]) continue;int j,t=0;for(j=1;j<=n;j++){if(mp[i][j]=='O') break;else if(mp[i][j]=='-') t++;}if(t==n){row[i]=1;continue;}if(j==(n+1)){//行i可清0row[i]=1;ch[cnt]='R';id[cnt]=i;for(int k=1;k<=n;k++)mp[i][k]='-';return 1;}}//列Ofor(int i=n;i>=1;i--){if(col[i]) continue;int j,t=0;for(j=1;j<=n;j++){if(mp[j][i]=='X') break;else if(mp[j][i]=='-') t++;}if(t==n){col[i]=1;continue;}if(j==(n+1)){//列i可清0col[i]=1;ch[cnt]='C';id[cnt]=i;for(int k=1;k<=n;k++)mp[k][i]='-';return 1;}}return 0;}int main(){cin>>tt;while(tt--){cin>>n;for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);memset(col,0,sizeof col);memset(row,0,sizeof row);int cnt=0,goal=1;while(1){if(check()) break;int t=find(cnt++);if(t==0){ goal=0;break;}}if(!goal){printf("No solution\n");continue;}for(int i=cnt-1;i>=0;i--){printf("%c%d",ch[i],id[i]);printf("%c",i?' ':'\n');}}return 0;}
0 0
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again (模拟)
- zoj 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again(topsort)
- zoj 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780Paint the Grid Again 模拟
- ZOJ 3780Paint the Grid Again
- ZOJ 3780 - Paint the Grid Again
- Paint the Grid Again ZOJ - 3780
- ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
- zoj 3780Paint the Grid Again 拓扑排序
- zoj 3780 Paint the Grid Again (拓扑排序)
- ZOJ 3780 --Paint the Grid Again 逆向自然求解法
- zoj 3780 Paint the Grid Again(拓扑排序)
- ZOJ-3780-Paint the Grid Again【11th浙江省赛】
- ZOJ 3780Paint the Grid Again-贪心模拟/拓扑排序
- ZOJ 3780 Paint the Grid Again (逆推)
- qt入门之窗口居中,没大小化按钮
- MySQL(基础篇)之触发器
- SPOJ Problem Set 2. Prime Generator 求某区间质数题解
- C++ - 显示接口&运行期多态 和 隐式接口&编译期多态
- LAMP配置详解
- zoj 3780 Paint the Grid Again
- RouterOS 自动拨号直至获得指定IP段IP的脚本
- The Singleton Pattern 单例模式
- android软引用和弱引用
- 什么叫事务
- java集合基础知识点总结
- myeclise设置文档注释
- 实现复数类中的友元函数运算符重载
- 工厂模式