同一个世界【c语言闯关】

来源:互联网 发布:mac电脑excel换行 编辑:程序博客网 时间:2024/05/01 18:27
#include <functional>#include <algorithm>#include <iostream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <stdlib.h>#include <cassert>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <ctype.h>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>const double PI = acos(-1.0);const double e = exp(1.0);const double eps=1e-8;inline int sign(double x){return (x>eps)-(x<-eps);}template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;}template<class T> T lcm(T a, T b) {return a / gcd(a, b) * b;}template<class T> inline T Min(T a, T b) {return a < b ? a : b;}template<class T> inline T Max(T a, T b) {return a > b ? a : b;}bool cmpbig(int a,int b) {return a > b;}bool cmpsmall(int a, int b) {return a < b;}/******************************************************/#define mem(a) memset(a,0,sizeof(a))#define pfd(a) printf("%d",a)#define pf2d(a,b) printf("%d %d",a,b)#define pf3d(a,b,c) printf("%d %d %d",a,b,c)#define pf4d(a,b,c,d) printf("%d %d %d %d",a,b,c,d)#define pfn printf("\n")#define pfdn(a) printf("%d\n",a)#define pf2dn(a,b) printf("%d %d\n",a,b)#define pf3dn(a,b,c) printf("%d %d %d\n",a,b,c)#define pf4dn(a,b,c,d) printf("%d %d %d %d\n",a,b,c,d)#define pfsn(a) printf("%s\n",a)#define sfd(a) scanf("%d",&a)#define sf2d(a,b) scanf("%d%d",&a,&b)#define sf3d(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sf4d(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)#define sfs(a) scanf("%s",a)#define sf    scanf#define pf    printf/******************************************************/#define ll long long#define sf64(a)    scanf("%lld",&a)#define sf264(a,b)    scanf("%lld%lld",&a,&b)#define sf364(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)#define sf464(a,b,c,d)    scanf("%lld%lld%lld%lld",&a,&b,&c,&d)#define pf64n(a) printf("%lld\n",a)#define pf264n(a,b) printf("%lld %lld\n",a,b)#define pf364n(a,b,c) printf("%lld %lld %lld\n",a,b,c)#define pf464n(a,b,c,d) printf("%lld %lld %lld %lld\n",a,b,c,d)#define pf64(a) printf("%lld",a)#define pf264(a,b) printf("%lld %lld",a,b)#define pf364(a,b,c) printf("%lld %lld %lld",a,b,c)#define pf464(a,b,c,d) printf("%lld %lld %lld %lld",a,b,c,d)#define read()freopen("1.txt","r",stdin);/******************************************************/const int dir4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};const int dir8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};#define N 1010#define M 1010#define MOD 1000000007#define INF 0x7fffffff/******************************************************/using namespace std;int n, m, ans;int a[N][N], b[N][N], ini[N][N];int ans1[N][N],ans2[N][N];bool vis1[N][N], vis2[N][N];int sum1, sum2;int be_x, be_y, en_x, en_y;struct node{int x,y,id;};stack<node> s1, s2;void init(){sum1 = 0;ans = 0;int flag = 0;for(int i = 1; i <= n; i ++){for(int j = 1; j <= m; j ++){a[i][j] = ini[i][j];if(a[i][j] == 1 || a[i][j] == 3)sum1 += 1;if(flag == 0 && (a[i][j] == 3 || a[i][j] == 2)){be_x = i;be_y = j;a[i][j] %= 2;flag = 1;}else if(flag == 1 && (a[i][j] == 3 || a[i][j] == 2)){en_x = i;en_y = j;a[i][j] %= 2;}}}mem(vis1);}void dfs2(int x,int y){if(ans == 1)return;vis2[x][y] = true;node nod;nod.x = x;nod.y = y;nod.id = s2.size() + 1;s2.push(nod);int be = b[x][y];b[x][y] = (b[x][y] + 1) % 2;sum2 += (b[x][y] - be);if(sum2 == 0){ans = 1;return ;}for(int i = 0; i < 4; i ++){if(ans == 1)return ;int nxt_x = x + dir4[i][0];int nxt_y = y + dir4[i][1];if(nxt_x < 1 || nxt_y < 1 || nxt_x > n || nxt_y > m || b[nxt_x][nxt_y] != 1)continue;if(vis2[nxt_x][nxt_y] == false){dfs2(nxt_x, nxt_y);}}if(ans == 1)return ;s2.pop();vis2[x][y] = false;be = b[x][y];b[x][y] = (b[x][y] + 1) % 2;sum2 += (b[x][y] - be);return ;}void check(){mem(vis2);sum2 = sum1;for(int i = 1; i <= n; i ++){for(int j = 1; j <= m; j ++){b[i][j] = a[i][j];}}dfs2(en_x, en_y);}bool j[N][N];queue<node> que;bool judge(){if(a[en_x][en_y] == 0)return false;mem(j);j[en_x][en_y] = true;int sz = 1;node aa;aa.x = en_x;aa.y = en_y;que.push(aa);while(!que.empty()){node no = que.front();que.pop();for(int i = 0; i < 4; i ++){int n_x = no.x + dir4[i][0];int n_y = no.y + dir4[i][1];if(a[n_x][n_y] == 1 && j[n_x][n_y] == false){node p;p.x = n_x;p.y = n_y;j[p.x][p.y] = 1;sz ++;que.push(p);}}}if(sz != sum1)return false;return true;}void dfs(int x,int y){if(ans == 1)return ;vis1[x][y] = true;node nod;nod.x = x;nod.y = y;nod.id = s1.size() + 1;s1.push(nod);int be = a[x][y];a[x][y] = (a[x][y] + 1) % 2;sum1 += (a[x][y] - be);if(judge())check();for(int i = 0; i < 4; i ++){if(ans == 1)return ;int nxt_x = x + dir4[i][0];int nxt_y = y + dir4[i][1];if(nxt_x<1||nxt_y<1||nxt_x>n||nxt_y>m||a[nxt_x][nxt_y]==-1)continue;if(vis1[nxt_x][nxt_y] == false){dfs(nxt_x, nxt_y);}}if(ans == 1)return ;s1.pop();vis1[x][y] = false;be = a[x][y];a[x][y] = (a[x][y] + 1) % 2;sum1 += (a[x][y] - be);return ;}void print(){pfsn("first");while(!s1.empty()){ans1[s1.top().x][s1.top().y] = s1.top().id;s1.pop();}for(int i = 1; i <= n; i ++){for(int j = 1; j <= m; j ++){printf("%3d",ans1[i][j]);}pfn;}pfsn("second");while(!s2.empty()){ans2[s2.top().x][s2.top().y] = s2.top().id;s2.pop();}for(int i = 1; i <= n; i ++){for(int j = 1; j <= m; j ++){printf("%3d",ans2[i][j]);}pfn;}}void scan(){for(int i = 1; i <=  n; i ++){for(int j = 1; j <= m; j ++){scanf("%d",&ini[i][j]);}}}void fun(){for(int i = 1; i <= n; i ++){for(int j = 1; j <= m; j ++){if(ini[i][j] == 1)ini[i][j] = 0;else if(ini[i][j] == 0)ini[i][j] = 1;else if(ini[i][j] == 2)ini[i][j] = 3;else if(ini[i][j] == 3)ini[i][j] = 2;}}init();dfs(be_x,be_y);}int main(){read();sf2d(n,m);scan();init();dfs(be_x, be_y);if(ans == 1){print();} else {fun();if(ans == 1)print();elsepfsn("No answer");}return 0;}


样例输入:

6 51 1 1 1 10 0 1 0 00 -1 1 -1 00 -1 1 -1 01 3 1 3 11 1 1 1 1


样例输出:

first  0  0  0  0  0  0  0 11 10  9  0  0 12  0  8  0  0 13  0  7  0  1 14 15  6  0  2  3  4  5second  0  0  0  0  0 13 12 11  0  0 14  0 10  0  0 15  0  9  0  0  0  7  8  1  2  0  6  5  4  3

0 0
原创粉丝点击