模拟拼图

题意:

  给定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. 【算法】【排序】【交换类】快速排序QuickSort

    #include<stdio.h> //快速排序 int main(){ ,,,,,,,,}; +; //基准指针 ; //慢指针 int* j=a; //快指针 int QS(int* ...

  2. 【Java例题】8.2 手工编写字符串统计的可视化程序

      2. 手工编写字符串统计的可视化程序. 一个Frame窗体容器,布局为null,两个TextField组件,一个Button组件. Button组件上添加ActionEvent事件监听器Actio ...

  3. Educational Codeforces Round 70 (Rated for Div. 2)

    这次真的好难...... 我这个绿名蒟蒻真的要崩溃了555... 我第二题就不会写...... 暴力搜索MLE得飞起. 好像用到最短路?然而我并没有学过,看来这个知识点又要学. 后面的题目赛中都没看, ...

  4. git常用指令整理

    git常用指令一览表 GIT指令 说明 git add . 将全部文件的内容加到Git索引以便执行commit. 这个指令不会检查文件夹中是否有文件被删除. 要注意的是,只有执行" git ...

  5. Java——检测其他线程的状态以及启动已死亡的线程

    这次这个的思路是在主类中维护一个map,map的key是线程名,value是线程的状态,然后创建周期执行的线程通过检测这个map来判断进程的状态,如果有死亡的进程就把该进程启动. 首先是主类,这里的m ...

  6. imageloader+图片压缩

    public class MainActivity extends AppCompatActivity { private ImageView ivIcon; @Override protected ...

  7. .netcore持续集成测试篇之Xunit结合netcore内存服务器发送post请求

    系列目录 Web项目中,很多与用户数据交互的请求都是Post请求,想必大家都用过HttpClient构造过post请求,这里并不对HttpClient做详细介绍,只介绍一些常用的功能.并结合AutoF ...

  8. CodeForces 15D Map

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译.(注意翻译里有错误,应该是优先选上面的矩阵,在同一行的优先选左边的矩阵) 这题一看就会做啊 (以下设大矩阵是\( ...

  9. Seq[找规律]----2019 年百度之星·程序设计大赛 - 初赛一:1005

    Seq Accepts: 1249 Submissions: 3956 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...

  10. var let及const

    es6已经发布很久了,之前只会用var定义变量,学习了let和const后,又学到了一些作用域.JavaScript编译和深拷贝浅拷贝的知识.这章主要来说说这三个定义量的方法: 1.var 在没学习e ...