模拟拼图

题意:

  给定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; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
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=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/*-----------------------showtime----------------------*/ const int maxn = 3e5+;
int a[maxn][];
int mp[maxn*][],vis[maxn];
vector<int>res[maxn]; int get(int id,int nd1,int nd2){
int res = -;
for(int i=; i<=; i++){
if(a[id][i] == nd1 && a[id][i+] == nd2)
{
res = i;
}
}
return res;
} int main(){
int n;
scanf("%d", &n);
int flag =, st = -;
for(int i=; i<=n; i++){
scanf("%d%d%d%d", &a[i][], &a[i][], &a[i][], &a[i][]);
a[i][] = a[i][];
a[i][] = a[i][];
a[i][] = a[i][];
a[i][] = a[i][];
int cnt = ;
for(int j=; j<=; j++){
if(a[i][j] == ) cnt++;
else {
if(mp[a[i][j]][] == ) mp[a[i][j]][] = i;
else if(mp[a[i][j]][] == )mp[a[i][j]][] = i;
// else { puts("impossible");return 0;}
}
}
if(cnt >= && st == -){
int tmp = get(i, , );
// debug(tmp);
if(tmp >=) st = i,a[st][] = tmp;
}
} if(st == -) {
puts("impossible");
return ;
}
int h=,w=;
w = ;
res[].pb(st);
vis[st] = ;
for(;;){
int id = res[][w];
int num = a[id][a[id][] + ];
if(num == ) break;
int nx = -;
if(vis[mp[num][]] == ) nx = mp[num][];
else if(vis[mp[num][]] == ) nx = mp[num][];
if(nx == -) {puts("impossible");return ;} int tmp = get(nx, , num);
if(tmp == -) {puts("impossible");return ;} a[nx][] = tmp;
res[].pb(nx); w++;
vis[nx] = ;
} for(; ;){
int id = res[h][];
int num = a[id][a[id][] + ];
// debug(num);
if(num == ) break;
int nx = -;
if(vis[mp[num][]] == ) nx = mp[num][];
else if(vis[mp[num][]] == ) nx = mp[num][]; if(nx == -) {puts("impossible");return ;} int tmp = get(nx, num, );
// debug(nx);
if(tmp == -) {puts("impossible");return ;} a[nx][] = tmp;
h++;
res[h].pb(nx);
vis[nx] = ;
} // cout<<h<<" "<<w<<endl;
for(int i=; i<=h; i++){
for(int j=; j<=w; j++){
int num1id = res[i-][j];
int num2id = res[i][j-]; int num1 = a[num1id][a[num1id][] + ];
int num2 = a[num2id][a[num2id][] + ];
// cout<<num1id<<" "<<num2id<<endl;
// cout<<num1<<" "<<num2<<endl;
if(num1 == || num2 == ) {puts("impossible"); return ;}
int nx1 = -, nx2 = -; if(vis[mp[num1][]] == ) nx1 = mp[num1][];
else if(vis[mp[num1][]] == ) nx1 = mp[num1][]; if(vis[mp[num2][]] == ) nx2= mp[num2][];
else if(vis[mp[num2][]] == ) nx2 = mp[num2][]; if(nx1 == - || nx2 == -) {puts("impossible");return ;}
if(nx1 != nx2) {puts("impossible");return ;} int tmp = get(nx1, num1, num2); if(tmp == -) {puts("impossible");return ;} a[nx1][] = tmp;
res[i].pb(nx1);
vis[nx1] = ;
if(i==h) if(a[nx1][tmp + ] != ) {puts("impossible");return ;}
if(j==w) if(a[nx1][tmp + ] != ) {puts("impossible");return ;} }
} if((h+) * (w + ) != n)
{puts("impossible");return ;} if(h == ) {
for(int i=; i<=w; i++) {
int id = res[][i];
int t = a[id][];
if(a[id][t+] != ) {puts("impossible");return ;}
if(i == w && a[id][t+] != ) {puts("impossible");return ;}
}
} if(w == ){
for(int i=; i<=h; i++){
int id = res[i][];
int t = a[id][];
if(a[id][t+] != ) {puts("impossible");return ;}
if(i == h && a[id][t+] != ) {puts("impossible");return ;} } }
printf("%d %d\n", h+, w+);
for(int i=; i<=h; i++){
for(int j=; j<=w; j++){
printf("%d ", res[i][j]);
}
puts("");
}
return ;
}

gym/102021/J GCPC18 模拟拼图的更多相关文章

  1. gym/102021/K GCPC18 背包dp算不同数和的可能

    gym/102021/K 题意: 给定n(n<=60)个直线 ,长度<=1000; 可以转化为取 计算 ans = (sum  + 10 - g) / ( n + 1)  在小于5的条件下 ...

  2. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  3. 【codeforces.com/gym/100240 J】

    http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...

  4. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  5. Gym 100851E Easy Problemset (模拟题)

    Problem E. Easy ProblemsetInput file: easy.in Output file: easy.outPerhaps one of the hardest problems ...

  6. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  7. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  8. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  9. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

随机推荐

  1. sift、surf、orb 特征提取及最优特征点匹配

    目录 sift sift特征简介 sift特征提取步骤 surf surf特征简介 surf特征提取步骤 orb orb特征简介 orb特征提取算法 代码实现 特征提取 特征匹配 附录 sift si ...

  2. 2019牛客多校训练第四场K.number(思维)

    题目传送门 题意: 输入一个只包含数字的字符串,求出是300的倍数的子串的个数(不同位置的0.00.000等都算,并考虑前导零的情况). sample input: 600 1230003210132 ...

  3. 【科研民工笔记2】Ubuntu 16.04 安装nvidia驱动

    我的主机是2060的显卡,用的是安装在U盘中的Ubuntu,开机进入后,因为没有安装驱动,所以界面看以来比较大. 通过手动方式,成功安装驱动,最终成功的方案使用的是run文件安装的方式. 1.手动下载 ...

  4. 【win】【qt5打包】【qt程序打包成一个可执行文件(带图标任何win都可以运行哦)】

    [前言] 业务需求将qt程序打包成win可执行文件.咱是做linux的,奈何用的麒麟系统,程序运行在win,好嘛,重新在win qtcreator编译后打包呗. [目标] 1.给qt程序添加一个图标. ...

  5. IDEA搭建工程

    1. 创建一个Project File -> New -> Project...   : 选择jdk版本,然后Next: 输入项目名,确定项目路径,Finish. 2. 创建一个Modul ...

  6. Sqlserver 锁表查询代码记录

    --方法1WITH CTE_SID ( BSID, SID, sql_handle ) AS ( SELECT blocking_session_id , session_id , sql_handl ...

  7. django前后端分离部署

    部署静态文件: 静态文件有两种方式1:通过django路由访问2:通过nginx直接访问 方式1: 需要在根目录的URL文件中增加,作为入口 url(r'^$', TemplateView.as_vi ...

  8. Hbase多版本(version)数据写入和读取

    1. 首先创建一个支持多版本的hbase表 create }   2.put几条测试数据 put ','f1:name','jack1' put ','f1:name','jack2' 3.读取多版本 ...

  9. 实时计算大数据处理的基石-Google Dataflow

    ​ 此文选自Google大神Tyler Akidau的另一篇文章:Streaming 102: The world beyond batch ​ 欢迎回来!如果您错过了我以前的帖子,Streaming ...

  10. 卷积神经网络cnn的实现

    卷积神经网络 代码:https://github.com/TimVerion/cat 卷积层 卷积层:通过在原始图像上平移来提取特征,每一个特征就是一个特征映射 原理:基于人脑的图片识别过程,我们可以 ...