ZOJ 3780Paint the Grid Again

来源:互联网 发布:尼康固件 镜头数据 编辑:程序博客网 时间:2024/06/01 09:46

倒着来就行  直接模拟


开始没注意逆序,分别用了优先队列,拓补排序都是wa

用优先队列应该时间会少很多


#include<iostream>#include<cstdio>#include<set>#include<string>#include<string.h>#include<cstring>#include<vector>#include<map>#include<queue>#include<stack>#include<cctype>#include<algorithm>#include<sstream>#include<utility>#include<cmath>#define mt(a) memset(a,0,sizeof (a))#define fl(a,b,c) fill(a,b,c)#define SWAP(a,b,t) (t=a,a=b,b=t)#define inf 1000000using namespace std;typedef long long ll;typedef struct node{int val;char type;}node;node ans[1000];char g[600][600];int n;stack<node>s;node cheak(){int flag = 0;node t;t.val = -1;for (int i = n-1; i>=0; i--){flag = 0;for (int j = 0; j < n; j++){if (g[j][i] == 'X'){flag = 0; break;}if (g[j][i] == 'O')flag = 1;}if (flag){t.type = 'C';t.val = i;return t;}}for (int i = n-1; i >=0; i--){flag = 0;for (int j = 0; j < n; j++){if (g[i][j] == 'O'){flag = 0; break;}if (g[i][j] == 'X')flag = 1;}if (flag){t.type = 'R';t.val = i;return t;}}return t;}int main(){int T;cin >> T;while (T--){while (!s.empty())s.pop();scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%s", g[i]);}node temp;while ((temp = cheak()), temp.val != -1){s.push(temp);if (temp.type == 'R'){for (int i = 0; i < n; i++){g[temp.val][i] = 'F';}}else{for (int i = 0; i < n; i++){g[i][temp.val] = 'F';}}}int flag = 1;for (int i = 0; i<n; i++){for (int j = 0; j < n; j++){if (g[i][j] != 'F'){flag = 0; break;}}}if (flag){printf("%c%d", s.top().type, s.top().val + 1);s.pop();while (!s.empty()){printf(" %c%d", s.top().type, s.top().val + 1);s.pop();}printf("\n");}else printf("No solution\n");}return 0;}


0 0
原创粉丝点击