UVa816 Abbott's Revenge

uva816 -- Abbott's Revenge (BFS) 题目在此 这一题是求最短路,肯定是使用BFS来求解,然而这一题我的AC过程很不顺利,起初是迷之Runtime Error,我以为是输出的时候采用递归导致栈溢出,改成循环后还是RE,后来不知道改了什么,终于不RE了,又开始了无限Wrong Answer,还好最后找出了WA的原因:是我对输入理解有误导致的,比如题目给出的测试迷宫SAMPLE中,有这样的一组数据:1 2 WLF NR ER * ,我起 阅读详情

        题意:走迷宫,在某个点,对于某个当前前进方向,只能往左、右、前三个方向中的某些方向走。

        思路:BFS。因为即使在同一个点,当前前进方向不同,能走的方向也不同,所以把一个点拆成四个来BFS,把每个点的前驱存起来,然后递归输出解即可。点的位置方向信息可以映射到一个整数,这样做比较方便。


#include <iostream>         
#include <stdio.h>         
#include <cmath>         
#include <algorithm>         
#include <iomanip>         
#include <cstdlib>         
#include <string>         
#include <memory.h>         
#include <vector>         
#include <queue>         
#include <stack>         
#include <map>       
#include <set>       
#include <ctype.h>         
#define INF 1000000010     
#define ll long long     
#define max3(a,b,c) max(a,max(b,c))     
#define MAXN 1000

using namespace std;     

string name;
int vis[4010];
int path[4010];
int cnt;

inline int char2int(char ch) {
	if(ch=='E'||ch=='F')return 0;
	if(ch=='S'||ch=='L')return 1;
	if(ch=='W'||ch=='R')return 2;
	if(ch=='N')return 3;
	return -1;
}

inline int hash(int r,int c,int dir){
	return r*40+c*4+dir;
}

inline void dehash(int n,int& r,int& c,int& dir){
	dir=n%4;
	n/=4;
	c=n%10;
	n/=10;
	r=n;
}

vector<int> data[10][10][5];

void print(int n){
	if(n==-1)return;
	print(path[n]);
	int a,b,c;
	dehash(n,a,b,c);
	if(!(cnt%10))printf("\n ");
	printf(" (%d,%d)",a,b);
	cnt++;
}

int main(){
	int startR,startC;
	int goalR,goalC;
	
	while(cin>>name){
		if(name=="END")break;
		memset(vis,0,sizeof(vis));
		memset(path,-1,sizeof(path));
		memset(data,0,sizeof(data));
		cnt=0;
		
		char ch;
		cin>>startR>>startC>>ch>>goalR>>goalC;
		data[startR][startC][char2int(ch)].push_back(0);
		
		int r,c;
		while(cin>>r){
			if(r==0)break;
			cin>>c;
			string str;
			while(cin>>str){
				if(str=="*")break;
				int dir=char2int(str[0]);
				
				for(int i=1;i<str.size();i++){
					data[r][c][dir].push_back( char2int(str[i]) );
				}
			}
		}
		
		bool ok=false;
		int end;
		int start=hash(startR,startC,char2int(ch));
		queue<int> que; que.push( start );
		vis[start]=1;
		int test=0;
		while(!que.empty()){
			
			int cur=que.front(); que.pop();
			int curR,curC,curD;
			
			dehash(cur,curR,curC,curD);
		
			if(curR==goalR&&curC==goalC&&test){
				ok=true;
				end=cur;
				break;
			}
			test++;
			for(int i=0;i<data[curR][curC][curD].size();i++){
				int tmpD;
				if(data[curR][curC][curD][i]==0){
					tmpD=curD;
				}
				if(data[curR][curC][curD][i]==1){
					tmpD=(curD+3)%4;
				}
				if(data[curR][curC][curD][i]==2){
					tmpD=(curD+1)%4;
				}
				
				int nn;
				if(tmpD==0){
					nn=hash(curR,curC+1,tmpD);
				}else if(tmpD==1){
					nn=hash(curR+1,curC,tmpD);
				}else if(tmpD==2){
					nn=hash(curR,curC-1,tmpD);
				}else if(tmpD==3){
					nn=hash(curR-1,curC,tmpD);
				}
				if(!vis[nn]){
					vis[nn]=true;
					que.push(nn);
					path[nn]=cur;
				}
			}
		}
		
		cout<<name;
		if(ok){
			print(end);
			printf("\n");
		}else{
			printf("\n  No Solution Possible\n");
		}
	}
	return 0;
}


UVa 816 Abbott的复仇 (Abbott's Revenge) 有一个最多包含9*9个交叉点的迷宫。输入起点,离开起点时的朝向和终点,求一条最短路(多解输出任意一个均可) 这个迷宫的特殊之处在于,进入一个交叉点的方向(用NEWS这四个字母分别表示北东西南,即上右左下)不同,允许出去的方向也不同。例如, 1 2 WLF NR ER * 表示交叉点(1,2)有三个路标,最后一个字符是结束标志,如果进入该点的方向是W,出去只能是L左转和F前进,其他同理。 要点: ... 阅读详情

相关推荐

Qt快速读取Excel文件

网上很多读取方式都是如下形式,太慢了,打开文件+读取文件要6s以上,实在是受不了! QAxObject excel("Excel.Application"); excel.setProperty("Visible", false); QAxObject *work_books = excel.querySubObject("WorkBooks"); work_boo...

c+c++的博客 2万+

[UVa 816] Abbott 的复仇(Abbott's Revenge)

Judge:https://vjudge.net/problem/UVA-816 题意:一个迷宫,每个点限制了从哪一方向(东西南北)来的,只能往左右前走,然后问起点到终点的最短路径。

StarlitNight的博客 596

从PWM到精准电流:双光耦与LM317构建的4-20mA隔离输出方案

本文详细介绍了基于双光耦与LM317的4-20mA隔离输出方案,适用于工业自动化中的PWM信号转换。该方案通过电气隔离和精准电流调节,显著提升抗干扰能力和信号传输精度,同时降低成本60%。文章还分享了电路设计、参数计算及调试经验,为工程师提供实用参考。

weixin_32006655的博客 283

例题:Abbott的复仇(UVa 816)

有一个最多包含9*9个交叉点的迷宫。输入起点、离开起点时的朝向和终点,求一条最短路(多解时任意输出一个即可) 这个迷宫的特殊之处在于:进入一个交叉点的方向(用NEWS这四个字母分别表示北东西南,即上下左右)不同,允许出去的方向也不同。例如,12 WLF NR ER*表示交叉点(1,2)(上数第一行,左数第二列)有3个路标(字符“*”只是结束标志),如果进入交叉点时的朝向为W(即朝左),则可以左转

Ethan的博客 922

土壤水动力学_前 言

Van Genuchten (V-G) 模型是土壤水分特征曲线中最常用的模型之一。它通过数学方程描述了土壤基质吸力与体积含水量之间的关系,被广泛应用于土壤水分运移、土壤盐渍化、边坡稳定性等研究领域。V-G模型是描述土壤水分特征曲线的重要工具,但它并不是完美的。在实际应用中,需要根据土壤类型、试验条件等因素选择合适的模型。土壤基质吸力是土壤水分学中的一个重要概念,它对土壤水分的保持、植物的生长以及土壤的工程性质都有重要影响。

weixin_44259522的博客 5327

EtherCAT主站开发(学习笔记一)

摘要:本文介绍了基于STM32H7的EtherCAT主站开发探索。EtherCAT是一种高实时性工业以太网协议,具有低延迟、高带宽利用率等特点。文章详细解析了EtherCAT的协议架构、帧结构和工作原理,重点阐述了PDO数据传输过程及主从站通信机制。同时介绍了CoE、FoE等应用层协议扩展。作者将基于SOEM开源协议栈进行开发,为后续实际应用中的伺服电机控制奠定基础。

weixin_69388007的博客 1475

uva816(迷宫最短路径困难版)

题面 在刘汝佳紫书p165 题意 给一个迷宫,告诉起点和终点坐标,但是和平时做的迷宫不一样,不是每个点都能走上下左右4个方向,规定了进入点的方向和从该点走出的方向(比如从北方进入a点能从a点向左或者向右转,但是从东方进入a点就只能直走),求从起点到终点的最短走法 思路 看到题目必然是bfs求最短路,但是这一堆条件把我搞懵了,用了半天才吃透这个题,首先,我们肯定要记录每个点的状态,行,列,朝向,转向,是否走过,距出发点距离,该点的父亲(以便输出结果),将朝北东南西(顺时针)走的坐标变化用二维数组表示,用一个函

tczz000的博客 447

基于图神经网络的社交网络虚假账号检测系统 (Graph-based Fake Account Detection System, GFADS)

FOLLOWSRETWEETSSHAREDCOMMENTSLIKESMENTIONSUseruser_idusernamejoin_datebioavatar_urllocationverifiedis_fakeLOAD CSV这是决定模型性能的关键环节,需结合和。XXedge_indexy新账号缺乏历史数据和丰富关系。全网图可能包含数亿节点和数十亿边,难以直接训练。社交网络是动态变化的,虚假账号策略也在不断进化。获取大规模、高质量的真实/虚假账号标注数据非常困难。

Deng945201314的博客 1481

uva 816 带方向的bfs

题意: 以下摘自小紫: 给一个9*9的迷宫,输入起点、离开起点时的朝向和终点,求一条最短路。(多解任意输出) 进入一个交叉点的方向(用NEWS这4个字母分别表示北东西南,即上右左下)不同,允许出去的方向也不同。 如图: 解析: 首先弄懂在每个点的状态有哪些:位置(x,y)朝向d,以此来建立节点,并且以此来建立bfs的状态step; 然后是方向dir与转弯turn的转换

渐近自由 612

1 电科院FTU检测标准学习笔记-外观检查

作者简介:本人从事电力系统多年,岗位包含研发,测试,工程等,具有丰富的经验在配电自动化验收测试以及电科院测试中,本人全程参与,积累了不少现场的经验。

sixtome 1852

PCL 计算点云包围球【2025最新版】

包围球通常用于简单排斥,因为与球体相交的方程很简单,同样重要的是,旋转一个球体并不会改变其范围。因此,当包围球用于简单排斥时,如果球体中心是对象的原点,则可以忽略对象的方向。

点云侠的博客 2068

UVA 816 Abbott's Revenge

思路: 来自:https://www.cnblogs.com/acm1314/p/4534360.html 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<queue> 5 using namespace std; 6 7 st...

qq_43119424的博客 129

linux v3h平台开发系列详解(GMSL摄像头篇):1.12 解串器MAX96712输出4 lane 变 2 lane的配置

本文详细介绍了在Linux v3h平台上,针对Max96712解串器进行MIPI 4lane到2lane配置。内容包括串行器解串器介绍、链路框架、硬件连接、以及软件寄存器如何配值和一些问题的解决方法。

xian18809311584的博客 1093

Abbott's Revenge(UVA816)

Abbott’s Revenge(UVA816) The 1999 World FinalsContest included a problem based on a “dicemaze.” At the time the problem was written, the judges were unable todiscover the original source of the dice m...

欢迎交流 261

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

uva 816 - Abbott's Revenge(bfs)

题目链接:uva 816 - Abbott's Revenge bfs,将每个点拆成四个点建图。 #include #include #include #include #include using namespace std; const int maxn = 30; const int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1},

不慌不忙、不急不躁 766

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

uva 816Abbott's Revenge

Description   Abbott’s Revenge  The 1999 World Finals Contest included a problem based on a “dice maze.” At the time the problem was written, the judges were unable to disc

渭城朝雨浥轻尘 1250

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Abbott 的复仇(Abbott's Revenge,ACM|IPPC World Finals 2000, UVa 816)

//算法竞赛入门经典中一道经典的用BFS求最短路径例题 //2015/01/30 #include #include #include #include #include using namespace std; typedef struct node //结点数据 { node(){} node(int row,int col,int d):r(row),c(col),dir(d){}

Doarcutine的专栏 1483

UVA816-Abbott's Revenge(搜索进阶)

ProblemUVA816-Abbott's Revenge Accept: 1010 Submit: 10466 Time Limit: 3000 mSec Problem Description The 1999 World Finals Contest included a problem based on a dice maze. At the time the...

weixin_30381793的博客 117

Uva-816 - Abbott's Revenge

Input The input file will consist of one or more arrow mazes. The first line of each maze description containsthe name of the maze, which is an alphanumeric string of no more than 20 characters. The

Russian_Blue_Cat的博客 1151

UVA816 Abbott's Revenge

vj上的题目链接:点击打开链接 最近在看刘汝佳的书,这题折腾了很久才理解 #include #include #include #include #include using namespace std; int r0,c0,dir,rs,cs,r2,c2; string mazeName; const char* dirs="NESW"; const char* turns="FLR";

JasonWu的博客 338

UVa 816 Abbott's Revenge【BFS】【迷宫】

https://vjudge.net/problem/UVA-816 分析输入: SAMPLE 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ELF * 2 3 SFR EL * 0 WLF的意思是,有个告示牌,从West西方向来的人能看到,他们可以选择向L和F走,也就是左转或者直行。 然后输...

之虎者也的博客 221

Abbott's Revenge UVA - 816

#include &lt;iostream&gt; #include &lt;bits/stdc++.h&gt; #define maxn 11 using namespace std; struct Node { int r,c,dir; Node(int r1,int c1,int dir1):r(r1),c(c1),dir(dir1){} Node(){} }; i...

剑似生平 184

UVA - 816 Abbott's Revenge

原题链接 分析:本题和普通的迷宫在本质上是一样的,但是由于“朝向”也起到了关键作用,所以需要用一个三元组(r,c,dir)表示“位于(r,c),面朝dir”这个状态。假设入口的位置为(r0,c0),朝向为dir,则初始状态并不是(r0,c0,dir),而是(r1,c1,dir),其中,(r1,c1)是(r0,c0)沿着方向dir走一步之后的坐标。此处用d[r][c][dir]表示初始状态

Epsilon 680
上一篇: UVa839 Not so Mobile
下一篇: UVa10305 Ordering Tasks
squee_spoon
博客等级 码龄14年 61粉丝 455原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值