gym/102021/J GCPC18 模拟拼图

模拟拼图

题意:

  给定n块拼图,每个拼图为四方形,对应四条边有四个数字,如果为0,表示这个边是在边界的,其他数字表示和另一个拼图的一条边相接。保证每个非零数只出现两次。

思路:

  模拟,但是要注意几个情况,第一就是只有一行或一列的时候,对于0的判断,还有就是拼图的个数要和n相等。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 9999973;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
/*-----------------------showtime----------------------*/

            const int maxn = 3e5+9;
            int a[maxn][10];
            int mp[maxn*4][2],vis[maxn];
            vector<int>res[maxn];

            int get(int id,int nd1,int nd2){
                int res = -1;
                for(int i=0; i<=3; i++){
                    if(a[id][i] == nd1 && a[id][i+1] == nd2)
                    {
                        res = i;
                    }
                }
                return res;
            }

int main(){
            int n;
            scanf("%d", &n);
            int flag =1, st = -1;
            for(int i=1; i<=n; i++){
                scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
                a[i][4] = a[i][0];
                a[i][5] = a[i][1];
                a[i][6] = a[i][2];
                a[i][7] = a[i][3];
                int cnt = 0;
                for(int j=0; j<=3; j++){
                    if(a[i][j] == 0) cnt++;
                    else {
                        if(mp[a[i][j]][0] == 0) mp[a[i][j]][0] = i;
                        else if(mp[a[i][j]][1] == 0)mp[a[i][j]][1] = i;
//                        else { puts("impossible");return 0;}
                    }
                }
                if(cnt >= 2 && st == -1){
                    int tmp = get(i, 0, 0);
                 //   debug(tmp);
                    if(tmp >=0) st = i,a[st][8] = tmp;
                }
            }

            if(st == -1) {
                puts("impossible");
                return 0;
            }
            int h=0,w=0;
            w = 0;
            res[0].pb(st);
            vis[st] = 1;
            for(;;){
                int id = res[0][w];
                int num = a[id][a[id][8] + 3];
                if(num == 0)  break;
                int nx = -1;
                if(vis[mp[num][0]] == 0) nx = mp[num][0];
                else if(vis[mp[num][1]] == 0) nx = mp[num][1];
                if(nx == -1) {puts("impossible");return 0;}

                int tmp = get(nx, 0, num);
                if(tmp == -1) {puts("impossible");return 0;}

                a[nx][8] = tmp;
                res[0].pb(nx); w++;
                vis[nx] = 1;
            }

             for(; ;){
                int id = res[h][0];
                int num = a[id][a[id][8] + 2];
            //    debug(num);
                if(num == 0) break;
                int nx = -1;
                if(vis[mp[num][0]] == 0) nx = mp[num][0];
                else if(vis[mp[num][1]] == 0) nx = mp[num][1];

                if(nx == -1) {puts("impossible");return 0;}

                int tmp = get(nx, num, 0);
              //  debug(nx);
                if(tmp == -1) {puts("impossible");return 0;}

                a[nx][8] = tmp;
                h++;
                res[h].pb(nx);
                vis[nx] = 1;
            }

       //    cout<<h<<" "<<w<<endl;
            for(int i=1; i<=h; i++){
                for(int j=1; j<=w; j++){
                    int num1id = res[i-1][j];
                    int num2id = res[i][j-1];

                    int num1 = a[num1id][a[num1id][8] + 2];
                    int num2 = a[num2id][a[num2id][8] + 3];
                 //   cout<<num1id<<" "<<num2id<<endl;
                 //   cout<<num1<<" "<<num2<<endl;
                    if(num1 == 0 || num2 == 0) {puts("impossible"); return 0;}
                    int nx1 = -1, nx2 = -1;

                    if(vis[mp[num1][0]] == 0) nx1 = mp[num1][0];
                    else if(vis[mp[num1][1]] == 0) nx1 = mp[num1][1];

                    if(vis[mp[num2][0]] == 0) nx2= mp[num2][0];
                    else if(vis[mp[num2][1]] == 0) nx2 = mp[num2][1];

                    if(nx1 == -1 || nx2 == -1) {puts("impossible");return 0;}
                    if(nx1 != nx2) {puts("impossible");return 0;}

                    int tmp = get(nx1, num1, num2);

                    if(tmp == -1) {puts("impossible");return 0;}

                    a[nx1][8] = tmp;
                    res[i].pb(nx1);
                    vis[nx1] = 1;
                    if(i==h) if(a[nx1][tmp + 2] != 0) {puts("impossible");return 0;}
                    if(j==w) if(a[nx1][tmp + 3] != 0) {puts("impossible");return 0;}

                }
            }

            if((h+1) * (w + 1) != n)
               {puts("impossible");return 0;}

            if(h == 0) {
                for(int i=0; i<=w; i++) {
                    int id = res[0][i];
                    int t = a[id][8];
                    if(a[id][t+2] != 0)  {puts("impossible");return 0;}
                    if(i == w && a[id][t+3] != 0)  {puts("impossible");return 0;}
                }
            }

            if(w == 0){
                for(int i=0; i<=h; i++){
                     int id = res[i][0];
                    int t = a[id][8];
                    if(a[id][t+3] != 0)  {puts("impossible");return 0;}
                    if(i == h && a[id][t+2] != 0)  {puts("impossible");return 0;}

                }

            }
            printf("%d %d\n", h+1, w+1);
            for(int i=0; i<=h; i++){
                for(int j=0; j<=w; j++){
                    printf("%d ", res[i][j]);
                }
                puts("");
            }
            return   0;
}

 

posted @ 2019-01-19 10:29  ckxkexing  阅读(286)  评论(0编辑  收藏  举报