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; //
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 模拟拼图的更多相关文章
- gym/102021/K GCPC18 背包dp算不同数和的可能
gym/102021/K 题意: 给定n(n<=60)个直线 ,长度<=1000; 可以转化为取 计算 ans = (sum + 10 - g) / ( n + 1) 在小于5的条件下 ...
- 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 ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Gym 100851E Easy Problemset (模拟题)
Problem E. Easy ProblemsetInput file: easy.in Output file: easy.outPerhaps one of the hardest problems ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces Gym 100500 J. Bye Bye Russia
Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
随机推荐
- Django使用本机IP无法访问,使用127.0.0.1能正常访问
使用Django搭建web站点后,使用127.0.0.1能访问,但是用自己本机IP却无法访问. 我们先到Django项目中找到setting文件 找到——> ALLOWED_HOSTS = [] ...
- Docker部署ELK 日志归集
ELK ELK是Elasticsearch.Logstash.Kibana的缩写,使用ELK的原因是因为公司使用Spring cloud部署了多个微服务,不同的微服务有不同的日志文件,当生产上出现问题 ...
- python 简单的实现文件内容去重
文件去重 这里主要用的是set()函数,特别地,set中的元素是无序的,并且重复元素在set中自动被过滤. 测试文本为 data.txt 具体代码如下: // 文件去重 #!/usr/bin/env ...
- java类加载器-Bootstrap、 ExtClassLoader、 AppClassLoader的关系
1. 简单介绍 Bootstrap. ExtClassLoader. AppClassLoader是java最根正苗红的类加载器. Bootstrap是本地代码编写的(例如C), ExtClassL ...
- java高并发系列 - 第22天:java中底层工具类Unsafe,高手必须要了解
这是java高并发系列第22篇文章,文章基于jdk1.8环境. 本文主要内容 基本介绍. 通过反射获取Unsafe实例 Unsafe中的CAS操作 Unsafe中原子操作相关方法介绍 Unsafe中线 ...
- Unity的弱联网Json数据传输
注意事项: 关于dictionary转json的工程中遇到一点问题:要手动添加双引号. 关于json转dictionary:同样需要手动去掉双引号,否则添加到dictionary中的字符串会带有双引号 ...
- Spring入门(八):自动装配的歧义性
1. 什么是自动装配的歧义性? 在Spring中,装配bean有以下3种方式: 自动装配 Java配置 xml配置 在这3种方式中,自动装配为我们带来了很大的便利,大大的降低了我们需要手动装配bean ...
- Android OTG之USB转串口模块通讯
微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521 1.背景简介 我们公司开发了一款室内机平板APP应用,要求平板能去控制智能门锁. ...
- 【KakaJSON手册】06_Model转JSON
前面的文章介绍了如何利用KakaJSON进行JSON转Model,从这篇文章开始介绍如何将Model转成JSON 生成JSON和JSONString struct Car: Convertible { ...
- 使用charls抓包微信小程序的解决方案(终极解决,各种坑不怕,亲测可用,不服来战!)
第一步:使用charles进行https抓包 https://www.jianshu.com/p/7a88617ce80b 使用charles进行https抓包 使用Charles进行HTTPS抓 ...