URAL 1920 Titan Ruins: the Infinite Power of Magic

2024-06-14 08:18

本文主要是介绍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 ;//奇偶剪枝 奇数returnfor(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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1059864

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1017. Staircases DP

1017. Staircases Time limit: 1.0 second Memory limit: 64 MB One curious child has a set of  N little bricks (5 ≤  N ≤ 500). From these bricks he builds different staircases. Staircase consist

ural 1026. Questions and Answers 查询

1026. Questions and Answers Time limit: 2.0 second Memory limit: 64 MB Background The database of the Pentagon contains a top-secret information. We don’t know what the information is — you

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

Spark MLlib模型训练—聚类算法 PIC(Power Iteration Clustering)

Spark MLlib模型训练—聚类算法 PIC(Power Iteration Clustering) Power Iteration Clustering (PIC) 是一种基于图的聚类算法,用于在大规模数据集上进行高效的社区检测。PIC 算法的核心思想是通过迭代图的幂运算来发现数据中的潜在簇。该算法适用于处理大规模图数据,特别是在社交网络分析、推荐系统和生物信息学等领域具有广泛应用。Spa

Keysight U8031A DC power supply

Keysight U8031A DC power supply 文章目录 Keysight U8031A DC power supply前言电容充电⽰意图一、恒定电压操作二、恒定电流操作三、5v操作四、跟踪模式操作五、存储器操作六、对过电压保护编程七、对过电流保护编程八、锁键操作 前言 U8031A Power Supply 是一款具备前面板编程能力的三路输出电源。通过使

PrimeTime low power-SMVA分析(4)

1.6使用示例 以下使用示例展示了SMVA流程: - 所有电压条件下的SMVA分析 - 特定DVFS约束下的SMVA分析 在以下脚本示例中,红色突出显示的文本显示了在SMVA流程中使用的命令、命令选项和变量。这些功能只有在将timing_enable_cross_voltage_domain_analysis变量设置为true时才能使用。 1.6.1所有电压条件下的SMVA分析 要对多

PrimeTime low power-SMVA分析(2)

1.4 DVFS 场景 对于使用动态电压和频率缩放(DVFS)的设计,可以使用 DVFS 场景来同时分析设计在所有 DVFS 条件下的性能。有关详细信息,请参见以下主题: - DVFS 场景概念 - 查询 DVFS 场景 - 将 DVFS 场景应用于命令和属性 - 与 DVFS 相关的对象属性 注意: DVFS 场景是在 SMVA 分析中使用的电压/频率场景。它们与分布式多场