模拟拼图

题意:

  给定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 ;
}

最新文章

  1. Java实现比较版本号
  2. [ROS]3 Linux编程练习
  3. ubuntu chm文档阅读
  4. Python 入门介绍
  5. Qt之QHeaderView加入复选框
  6. RobotFramework - AppiumLibrary 之关键字Open Application使用
  7. Python之推导式、生成器表达式
  8. Spring Boot, Java Config - No mapping found for HTTP request with URI [/…] in DispatcherServlet with name &#39;dispatcherServlet&#39;
  9. [从Paxos到ZooKeeper][分布式一致性原理与实践]&lt;二&gt;一致性协议[Paxos算法]
  10. Android EventBus3.x 使用详解
  11. D3.js的基础部分之选择集的处理 enter和exit的处理方法 (v3版本)
  12. HBase操作(Shell与Java API)
  13. spring jpa exists
  14. bootstrap -- css -- 图片
  15. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】
  16. Arm-kernel 内存收集【转】
  17. CSS 左边div固定,右边div自适应
  18. nextcloud 安装
  19. 查看selinux的状态
  20. web.xml上监听器作用

热门文章

  1. 11. Java常用类
  2. [NSNull intValue]: unrecognized selector sent to instance 0x375c9860
  3. 入门MySQL——基础语句篇
  4. JAVA-Spring AOP五大通知类型
  5. 免安装版tomcat安装成服务
  6. redis分布式锁&amp;队列应用
  7. win系统上Anaconda国内镜像配置
  8. HelloDjango 系列教程:博客从“裸奔”到“有皮肤”
  9. Flutter学习笔记(14)--StatefulWidget简单使用
  10. SpringBoot:如何优雅地处理全局异常?