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 ...
随机推荐
- Linux学习笔记04
文件查找命令find 文件查找命令: which locate find which:查找命令字所在的位置 locate:模糊匹配(只要包含关键字的文件都查找出来) 不是实时的,基于数据库查找, up ...
- request获取url链接和参数
//Returns the part of this request's URL from the protocol name up to the query string in th ...
- extjs4 表单验证自定义
extjs4 在验证上面支持的也特别好,他可以使用自带的格式验证,也可以自定义验证 比如:正则验证,密码重复填写对比验证,以及 调用后台方法验证,下面将验证方法统一写出以供参考 function lo ...
- 2019牛客多校训练第三场B.Crazy Binary String(思维+前缀和)
题目传送门 大致题意: 输入整数n(1<=n<=100000),再输入由n个0或1组成的字符串,求该字符串中满足1和0个数相等的最长子串.子序列. sample input: 801001 ...
- red hat enterprise Linux 64 bit 配置IP
在win7 64位操作系统的台式机器上,安装了VMware® Workstation,9.0.1 build-894247.新建一个虚拟机安装linux.具体过程请搜索相关文档.安装的时候选择的网络连 ...
- SpringBoot操作ES进行各种高级查询
SpringBoot整合ES 创建SpringBoot项目,导入 ES 6.2.1 的 RestClient 依赖和 ES 依赖.在项目中直接引用 es-starter 的话会报容器初始化异常错误,导 ...
- Spring基础笔记
Spring带给了我们什么便利? 注解版本的IOC如何玩? 组件注册 组件注册的过程中有哪些过滤规则? 如何控制组件的作用域(单例多例)? 六种注册组件的方式? 生命周期 什么是bean的生命周期 在 ...
- 在github上搭建个人博客并在线更新
换博客比更博还勤的我终于决定写一篇博客搭建教程了.. FAQ Q:\(hexo\)需要本地编译.\(jekyll\)虽然可以直接上传\(md\)..但是如果在github上直接编译也太难受了叭,毕竟不 ...
- 线程学习oneday
进程:执行中的程序叫做进程(Process),是一个动态的概念. 线程:一个进程可以产生多个线程.同多个进程可以共享操作系统的某些资源一样,同一进程的多个线程也可以共享此进程的某些资源(比如:代码.数 ...
- Top11 构建和测试API的工具
立刻像专业人士一样构建API 组织正在改变他们已经在软件应用项目中成功的微服务架构模型,这就是大多数微服务项目使用API(应用程序接口)的原因. 我们要为微服务喝彩,因为它相对于其他的模型有各种先进的 ...