一、题目大意

Intersections of Crossing Path City are aligned to a grid. There are N east-west streets which are numbered from 1 to N, from north to south. There are also N north-south streets which are numbered from 1 to N, from west to east. Every pair of east-west and north-south streets has an intersection; therefore there are N2 intersections which are numbered from 1 to N2.
Surprisingly, all of the residents in the city are Ninja. To prevent outsiders from knowing their locations, the numbering of intersections is shuffled.
You know the connections between the intersections and try to deduce their positions from the information. If there are more than one possible set of positions, you can output any of them.

对于一个矩阵,给出矩阵中每个节点之间的相互连接关系,要求还原矩阵。

二、思路

观察可得,对于任意一个矩阵,四个拐角处的元素都有一个特点——度为2。因此可以考虑,从外向里一圈一圈的放置元素,在一轮放置结束后,将其他节点的度作相应调整——所有与本轮中放置的节点相关联的节点度数减一。

则可以得到新的拐角元素。对于每一层,都可以枚举度小于2<考虑奇数宽度最中间的一个元素度为0>的元素的起始位置。则也因此可以由节点开始遍历本层元素。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<stack>
#include<vector>
#include<string>
#include<string.h>
#include<queue>
using namespace std; #define ll intmax_t
#define pp pair<int,int>
#define veci vector<int> const int MAXN=;
int mapp[MAXN][MAXN];
pp coors[MAXN*MAXN];
int n,summ,s_ele;
int du[MAXN*MAXN];
veci G[MAXN*MAXN];
veci que; int addx[]={,,,-};
int addy[]={,,-,}; void show()
{
for(int i=;i<n;++i)
{
for(int j=;j<n;++j)
{
cout<<mapp[i][j];
if(j == n-)cout<<endl;
else cout<<" "; }
}
} void mark(int now,int x,int y)
{
que.push_back(now);
coors[now] = make_pair(x,y);
mapp[x][y] = now;
summ--;
} bool check_legal(int x,int y,int now)
{
int len = G[now].size(); if(x<||x>=n||y<||y>=n)return false;
if(mapp[x][y] != -)return false; for(int i=;i<;++i)
{
int tx = x + addx[i];
int ty = y + addy[i];
if(tx<||tx>=n||ty<||ty>=n)continue;
if(mapp[tx][ty] == -)continue;
bool succ = ;
for(int j=;j<len;++j)
{
int tar = G[now][j];
if(mapp[tx][ty] == tar)
{
succ = ;
break;
}
}if(!succ)return false;
}return true;
} void set_first(int now,int dep)
{
int d1 = dep;
int d2 = n--dep;
int x[] = {d1,d1,d2,d2};
int y[] = {d1,d2,d1,d2};
for(int i=;i<;++i)
{
if(check_legal(x[i],y[i],now))
{
mark(now,x[i],y[i]);
break;
}
}
} bool check_dep(int x,int y,int dep)
{
if(x<||x>=n||y<||y>=n)return false;
int d1 =dep;
int d2 = n--dep;
return x == d1 || x == d2 || y == d1 || y == d2;
} void dfs(int now,int dep)
{
// cout<<now<<endl;
int x = coors[now].first;
int y = coors[now].second;
pp next_coors[];
for(int i=;i<;++i)
{
next_coors[i] = make_pair(x+addx[i],y+addy[i]);
}
int len = G[now].size();
for(int i=;i<len;++i)
{
int tar = G[now][i];
if(coors[tar].first != -)continue;
if(du[tar]==)continue;
for(int j=;j<;++j)
{
int tx = next_coors[j].first;
int ty = next_coors[j].second;
if(check_dep(tx,ty,dep)&&check_legal(tx,ty,tar))
{
mark(tar,tx,ty);
dfs(tar,dep);
break;
}
}
}
} void flush_du()
{
int len = que.size();
for(int i=;i<len;++i)
{
int now = que[i];
int len1 = G[now].size();
for(int j = ;j<len1;++j)
{
int tar = G[now][j];
du[tar] -- ;
if(du[tar] == )s_ele = tar;
}
}
} void init()
{
memset(mapp,-,sizeof(mapp));
int len = *n*n -*n;
for(int i=;i<len;++i)
{
int a,b;
cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
summ = len = n*n;
for(int i=;i<=len;++i)
{
coors[i] = make_pair(-,-);
du[i] = G[i].size();
if(du[i] == )s_ele = i;
}
int dep = ;
while(summ )
{
que.clear();
set_first(s_ele,dep);
dfs(s_ele,dep);
flush_du();
dep ++;
}
show();
} int main()
{
cin.sync_with_stdio(false);
while(cin>>n)init(); return ;
}

HNU暑假训练第一场C.Ninja Map的更多相关文章

  1. 19暑假多校训练第一场-J-Fraction Comparision(大数运算)

    链接:https://ac.nowcoder.com/acm/contest/881/J来源:牛客网 题目描述 Bobo has two fractions xaxa and ybyb. He wan ...

  2. Gym-101653:acific Northwest Regional Contest (2019训练第一场)

    本套题没有什么数据结构题,图论题,唯一有价值的就是Q题博弈,在最后面,读者可以直接拉到最下面. (还剩下两个,估计每什么价值的题,懒得补了 M .Polyhedra pro:欧拉公式,V-E+F=2: ...

  3. 大家一起做训练 第一场 E Number With The Given Amount Of Divisors

    题目来源:CodeForce #27 E 题目意思和题目标题一样,给一个n,求约数的个数恰好为n个的最小的数.保证答案在1018内. Orz,这题训练的时候没写出来. 这道题目分析一下,1018的不大 ...

  4. 大家一起做训练 第一场 B Tournament

    题目来源:CodeForce #27 B 有n个人比赛,两两之间都有一场比赛,一共 n * (n - 1) / 2 场比赛.每场比赛的记录方式是 a b,表示在a和b的比赛中,a胜出,b失败. 经过研 ...

  5. 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)

    链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...

  6. 牛客网多校训练第一场 D - Two Graphs

    链接: https://www.nowcoder.com/acm/contest/139/D 题意: 两个无向简单图都有n(1≤n≤8)个顶点,图G1有m1条边,图G2有m2条边,问G2有多少个子图与 ...

  7. HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 2015多校联合训练第一场Tricks Device(hdu5294)

    题意:给一个无向图,给起点s,终点t,求最少拆掉几条边使得s到不了t,最多拆几条边使得s能到t 思路: 先跑一边最短路,记录最短路中最短的边数.总边数-最短边数就是第二个答案 第一个答案就是在最短路里 ...

  9. SDU暑假排位第一场 (Gym - 100889)

    啊今天有点挂机啊 D题和队友暴力后发现一组数据跑得飞快 然后遇上1e5组数据就没了..... 然后我疯狂优化暴力 然后去世了 最后半小时F也没写出来 主要还是最后有点慌并且没有考虑清楚 导致情况越写越 ...

随机推荐

  1. JavaScript写入文件到本地

    工作中有时需要通过 JavaScript 保存文件到本地,我们都知道 JavaScript 基于安全的考虑,是不允许直接操作本地文件的.IE 可以通过 VB 插件的方式进行,而 Chrome 和 fi ...

  2. 在thinkpad SL400上U盘安装双系统ubuntu14.10

    转自:http://zydky.iteye.com/blog/1674100 上文中装的双系统是centos6.3,因为自己对ubuntu有点熟悉,就装了ubuntu. 笔记本是09年入手的,买了之后 ...

  3. Java—继承

    继承 继承是类与类的一种关系,是一种“is a”的关系.注意:java中的继承是单继承,一个类只有一个父类. 继承的好处:子类拥有父类的所有属性和方法(private修饰的无效),实现代码的复用 语法 ...

  4. Linux系统如何设置开机程序自启动

    在Linux系统如何让程序开机时自动启动      核心提示:系统的服务在开机时一般都可以自动启动,那在linux系统下如果想要程序在开机时自动启动怎么办?我们知道在 windows系统“开始”--& ...

  5. 百万级数据库SQL优化大总结

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...

  6. DEEP LEARNING 大满贯课程表

    Reinforcement Learning post by ISH GIRWAN Courses/Tutorials Deep Reinforcement Learning, Spring 2017 ...

  7. python数组列表、字典、拷贝、字符串

    python中字符串方法 name = "I teased at life as if it were a foolish game" print(name.capitalize( ...

  8. QT Creater 配色方案及下载

    打开QT Creater的工具--选项--文本编辑器--字体和颜色,复制一份配色方案:Vim (dark) ->Vim (dark) (copy) 更改想更改的任何内容的配色.其中,修改后的文件 ...

  9. 是否应该提供一个dao.insertIgnoreNull ? (像updateIgnoreNull一样)

     是否应该提供一个dao.insertIgnoreNull ? (像updateIgnoreNull一样)  发布于 406天前  作者 SayingCode  153 次浏览  复制  上一个帖子  ...

  10. 使用selenium grid与BrowserMobProxyServer联合使用

    背景:项目主要是做埋点数据,要使用 BrowserMobProxyServer,它相当于做一个代理,在你访问一个网页时,通过代理,获取打开网页的数据,对比你需要对比数据,所以这个工具提供获取页面请求的 ...