URAL 1920 Titan Ruins: the Infinite Power of Magic
大意:
有一张N*N的网格,你每次可以走一步,每格只能走一次,有没有一种方法让走了L步后回到一个距原点1步远的格子?
没有输出Unsuitable device,否则输出Overwhelming power of magic并输出方案。
一开始用DFS 奇偶剪枝了还是TLE,
代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#define N 110
using namespace std; int n,t,end_i,end_j;
bool visited[N][N],flag,ans;
char map[N][N];
int run[10010][2];
int tem;
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; void DFS(int i,int j,int c)
{
if(flag){ return ;}
if(c>t) return ;
if(i<=0||i>n||j<=0||j>n) {return ;}
if(map[i][j]=='D'&&c==t) {flag=ans=true; return ;}
int temp=abs(i-end_i)+abs(j-end_j);
temp=t-temp-c; //t扣掉还要走的最短步temp 和 已经走过的 c 如果这些步还是奇数直接不满足
if(temp&1) return ;//奇偶剪枝 奇数return
for(int k=0;k<4;k++)
if(!visited[i+a[k][0]][j+a[k][1]]) //开始进行各个方向的探索 记得回溯,取消之前走的状态
{
visited[i+a[k][0]][j+a[k][1]]=true;
DFS(i+a[k][0],j+a[k][1],c+1);
if(flag){cout<<i+a[k][0]<<" "<<j+a[k][1]<<endl;break;}
visited[i+a[k][0]][j+a[k][1]]=false;
}
} int main()
{
int i,j;
while(cin>>n>>t)
{
if(t%2!=0||t>n*n){cout<<"Unsuitable device"<<endl;continue;}
else cout<<"Overwhelming power of magic"<<endl;
cout<<"1 1"<<endl;
memset(visited,0,sizeof(visited)); for(i=1;i<=n;i++)
{ for(j=1;j<=n;j++)
{
map[i][j]='.';
}
}
end_i=2;end_j=1;
map[2][1]='D';
visited[1][1]=1; ans=flag=false; DFS(1,1,1);
// if(ans) cout<<"YES"<<endl;
// else cout<<"NO"<<endl;
}
return 0;
}
后来是找规律做出来的,根据奇偶分类讨论一下;;
代码写的很烂。。
很多复用的没复用。。
#include<iostream>
#include<cstring>
#include<cmath>
#define N 110
using namespace std; int n,t,end_i,end_j;
bool visited[N][N],flag,ans;
char map[N][N];
int run[10010][2];
int tem;
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int main()
{
int i;int xx,yy,kk;
while(cin>>n>>t)
{
if(t%2!=0||t>n*n){cout<<"Unsuitable device"<<endl;continue;}
else cout<<"Overwhelming power of magic"<<endl;
// cout<<"1 1"<<endl;
if(n%2==0){
if(t<=2*n){
for(i=1;i<=t/2;i++){
cout<<1<<" "<<i<<endl;
}
for(i=t/2;i>0;i--){
cout<<2<<" "<<i<<endl;
}
}
else{
kk=t-2*n;
yy=kk/(2*(n-2));
xx=kk%(2*(n-2));
for(i=1;i<=n;i++){
cout<<"1 "<<i<<endl;
}
if(xx!=0){
int w2=n-2;
for( i=2;i<=2+xx/2;i++){
cout<<i<<" "<<n<<endl;
}
for( i=2+xx/2;i>=2;i--){
cout<<i<<" "<<n-1<<endl;
}
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<w2<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<w2-1<<endl;
w2-=2;
}
while(w2!=0){ cout<<"2 "<<w2<<endl;
cout<<"2 "<<w2-1<<endl;
w2-=2;
}
}
else{
int wei;
wei=n;
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<wei<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<wei-1<<endl;
wei-=2;
}
while(wei!=0){ cout<<"2 "<<wei<<endl;
cout<<"2 "<<wei-1<<endl;
wei-=2;
}
}
}
}
else{ if(t<=2*n){
for(i=1;i<=t/2;i++){
cout<<1<<" "<<i<<endl;
}
for(i=t/2;i>0;i--){
cout<<2<<" "<<i<<endl;
}
}
else if(t<=2*n+2*(n-2)){
int er=(t-(2*n))/2;
for(i=1;i<=n;i++){
cout<<1<<" "<<i<<endl;
}
for(i=n;i>=3;i--){
cout<<2<<" "<<i<<endl;
}
for(i=2;i<=2+er;i++){
cout<<i<<" "<<2<<endl;
}
for(i=er+2;i>=2;i--){
cout<<i<<" "<<1<<endl;
} }
else if(t<=(n+n-1+3*(n-2))){
int bb=(t-(2*n+2*(n-2)))/2;
for(i=1;i<=n;i++){
cout<<1<<" "<<i<<endl;
}
for(i=n;i>=3;i--){
cout<<2<<" "<<i<<endl;
}
for(i=3;i<=n;i++){
cout<<i<<" "<<3<<endl;
}
cout<<n<<" "<<2<<endl;
int x=n,y=1;
cout<<x<<" "<<y<<endl;
x--;
cout<<x<<" "<<y<<endl; while(1){
if(x==2&&y==1){break;}
if(bb>0&&y==1&&x%2==0){
y=2;bb--;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==0){
x--;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==1){
y=1;cout<<x<<" "<<y<<endl;
}
else if(y==1&&x%2==1){
x--;
cout<<x<<" "<<y<<endl;
}
else if(bb<=0&&y==1&&x%2==0){
x--;
cout<<x<<" "<<y<<endl;
}
}
}
else{
kk=t-(n+n-1+3*(n-2));
yy=kk/(2*(n-2));
xx=kk%(2*(n-2));
for(i=1;i<=n;i++){
cout<<"1 "<<i<<endl;
}
if(xx!=0){
int w21=n-2;
for( i=2;i<=2+xx/2;i++){
cout<<i<<" "<<n<<endl;
}
for( i=2+xx/2;i>=2;i--){
cout<<i<<" "<<n-1<<endl;
}
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<w21<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<w21-1<<endl;
w21-=2;
}
while(w21!=3){ cout<<"2 "<<w21<<endl;
cout<<"2 "<<w21-1<<endl;
w21-=2;
}
}
else{
int wei2;
wei2=n;
while(yy--){ for(i=2;i<=n;i++)
cout<<i<<" "<<wei2<<endl;
for(i=n;i>=2;i--)
cout<<i<<" "<<wei2-1<<endl;
wei2-=2;
}
while(wei2!=3){ cout<<"2 "<<wei2<<endl;
cout<<"2 "<<wei2-1<<endl;
wei2-=2;
}
}
/*
*
*/
for(i=2;i<=n;i++){
cout<<i<<" "<<3<<endl;
}
cout<<n<<" "<<2<<endl;
int x=n,y=1;
cout<<x<<" "<<y<<endl;
x--;
cout<<x<<" "<<y<<endl; while(1){
if(x==2&&y==1){break;}
if(y==1&&x%2==0){
y=2;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==0){
x--;
cout<<x<<" "<<y<<endl;
}
else if(y==2&&x%2==1){
y=1;cout<<x<<" "<<y<<endl;
}
else if(y==1&&x%2==1){
x--;
cout<<x<<" "<<y<<endl;
} } } } }
return 0;
}
有的队也有用DFS过的T T
#include<iostream>
using namespace std;
int n,l,ans;
void dfs(int x,int y,int z){
if(ans==l) return ;
if(x>n||y>n) return ;
if(z==0){
printf("%d %d\n",x,y);
ans+=2;
dfs(x+1,y,0);
printf("%d %d\n",x,y+1);
if(n%2==x%2) dfs(x,3,1);
}
else if(z==1){
printf("%d %d\n",x,y);
ans+=2;
dfs(x,y+1,1);
printf("%d %d\n",x-1,y);
if(x==3&&y%2==0) dfs(x-2,y,2);
}
else if(z==2){
ans+=2;
printf("%d %d\n",x,y);
printf("%d %d\n",x,y-1);
}
}
int main(){
cin>>n>>l;
if(l%2==1||n*n<l){
printf("Unsuitable device\n");
}
else{
printf("Overwhelming power of magic\n");
printf("1 1\n");
printf("2 1\n");
ans=4;
dfs(3,1,0);
printf("2 2\n");
if(ans!=n) dfs(2,3,1);
printf("1 2\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
URAL 1920 Titan Ruins: the Infinite Power of Magic的更多相关文章
- URAL - 1920 Titan Ruins: the Infinite Power of Magic(乱搞)
搞死人的题目,,, 就是在n*n的方格中找路径长度为L的回路. 开始的思路值适合n为偶数的情况,而忽视了奇数的case,所以wa了一次. 然后找奇数case的策略,代码从70多行变成了100多,然后改 ...
- URAL - 1917 Titan Ruins: Deadly Accuracy(水题)
水题一个,代码挫了一下: 题意不好理解. 你去一个洞窟内探险,洞窟内有许多宝石,但都有魔法守护,你需要用魔法将它们打下来. 每个宝石都有自己的防御等级,当你的魔法超过它的防御等级时它就会被你打下来. ...
- URAL 1915 Titan Ruins: Reconstruction of Bygones(思路)
搞这个题差不多是从比赛开始到结束. 从自信慢慢的看题一直到wrong到死. 这个题目可以说成是思路题,以为我们只要明白一点,这道题就成了纯暴力的水题, 那就是当操作数不足栈中数字数目的时候,我们就没有 ...
- 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)
题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...
- 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- OrientDB入门(1)Getting Started
Running OrientDB the First Time First, download and extract OrientDB by selecting the appropriate pa ...
- Type curtilage home
This year's National Day coincides with the Mid-Autumn festival, the double false merger about eight ...
- HDU5840(SummerTrainingDay08-B 树链剖分+分块)
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 5840 This world need more Zhu 树链剖分+暴力
This world need more Zhu 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5840 Description As we all ...
随机推荐
- Custom Sort Order
When trying to sort based on values that do not fit the standard ascending and descending sort logic ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- php中利用正则去掉中文全角空格
一开始用$temp = trim($temp, " "); 这种方法,导致trim后的中文字符有乱码 最后 $str = " 广东君孺律师事务所 "; $str ...
- 强大的网络通信框架(实现缓存)--第三方开源--volley
Android Volley是Android平台上很好用的第三方开源网络通信框架.使用简答,功能强大. Android Volley的库jar包Volley.ja下载连接地址:Volley下载 下载后 ...
- 如何解决加载动态链接库DLL失败,返回0,GetLastError返回错误码126
通常情况下使用LoadLibrary加载DLL都可以成功,但是当被加载的DLL内部依赖其他DLL无法被找到时,该函数会返回126(ERROR_MOD_NOT_FOUND)错误. 解决办法有2种: 1) ...
- 对"使用Mono Runtime Bundle制作安装包让C#桌面应用程序脱离net framework"增加说明
http://www.cnblogs.com/basilwang/archive/2011/11/29/2267809.html 想做独立引用的估计都看过这一篇文章,但是因为软件更新,很多地方已经不适 ...
- MongoDB 学习笔记(三)—— 修改器的使用
通常文档只会有一部分数据要更新,所以使用修改器来操作文档极为高效. 小技巧:了解函数功能,不带括号即可.如:db.blog.update即可查看update函数的具体参数和方法体. $set修改器 & ...
- Python脚本控制的WebDriver 常用操作 <十> 层级定位
下面将使用WebDriver来模拟操作一个层级定位元素的操作 测试用例场景 在实际的项目测试中,经常会有这样的需求:页面上有很多个属性基本相同的元素,现在需要具体定位到其中的一个.由于属性基本相当,所 ...
- python-抓取图片
今天看到博客园一个文章,python抓取图片,也没看内容,心想自己也写一个抓取脚本试试看,一方面自己也在学习python,另一方面毕竟实际工作也经常会遇到这种需要临时写脚本的时候,突击锻炼还是好的嘛. ...
- OC编写使用调试器
OC编写使用调试器 编写代码免不了,Bug.那么Debug就是程序员的必备技能了.本文和大家一起探讨,如何在应用开发编写代码过程中,使用日志项消息:以及使用动作.条件.迭代控制增强断点. 记录信息 在 ...