当前位置:网站首页>UVA816 Abbott’s Revenge
UVA816 Abbott’s Revenge
2022-06-25 05:11:00 【Linn21】
Title



Ideas
By using BFS Traverse the maze to find the shortest path .for Loop simulation goes in three directions . If you can walk in one direction , Record where you went , Join the queue , So that we can find our way to it later . Record its previous node , And calculate the distance , The distance to the node is the distance from the previous node plus one .
for (int i = 0; i < 3; i++) // Straight left and right
{
node gone = walk(now, i); // Suppose you take one step
// The first condition determines whether the steering is allowed , The second condition determines whether the coordinates are in the maze , The third coordinate determines whether the point has passed
if (maze[now.row][now.col][now.dir][i] && legal(gone.row, gone.col) && dis[gone.row][gone.col][gone.dir] == -1)
{
q.push(gone); // Join the queue for the next step BFS
// Record the distance , Pre record node
dis[gone.row][gone.col][gone.dir] = dis[now.row][now.col][now.dir] + 1;
nbefore[gone.row][gone.col][gone.dir] = now;
}walk Function turn value 0 1 2 Respectively : Go straight on 、 Turn left 、 Right turn . Go straight in the same direction , So there is no need to be right dir Deal with .
For direction string const char *dirs = "NESW";dir Is its numerical representation . Turn left ( Suppose it is E, Turn left and then N) Then this function can dir Deal with it as dirs The previous digit of the value represents . The same goes for the right .
node walk(const node &n, int turn)
{
int dir = n.dir;
switch (turn)
{
case 1:
dir = (dir + 3) % 4; // Turn left to update the direction dirs Previous , Anticlockwise
break;
case 2:
dir = (dir + 1) % 4; // Turn right to update the direction dirs The latter one , clockwise
}
return node(n.row + rc[dir][0], n.col + rc[dir][1], dir);
}- utilize int dir_id(char ch), int turn_id(char ch) The function converts the direction and direction of the maze into a numerical representation .
- bool maze[10][10][4][3] It is used to represent the maze abstractly , The first two dimensions are row and column coordinates , The third dimension represents the orientation of the current position , The fourth dimension represents steering . That is, if an array value is true , Indicates that you can turn to a certain direction in the current row and column coordinates .
- int dis[10][10][4] Indicates the distance between the current coordinate and the current orientation relative to the starting point .
- node nbefore[10][10][4] It is used to record the current coordinates and the previous node on the path of the current orientation . So that when outputting , Follow nbefore Data backward path of .
- node walk(const node &n, int turn) It can represent a step forward in the maze
Refer to liurujia 《 Classic introduction to algorithm competition ( The second edition )》
#include <iostream>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
struct node
{
int row, col, dir;
node(int r = -1, int c = -1, int d = -1) : row(r), col(c), dir(d) {}
};
bool maze[10][10][4][3]; // Describe the maze
int dis[10][10][4]; // Distance to entrance
node nbefore[10][10][4]; // Save the previous node
const char *dirs = "NESW"; // North, Southeast and West
const char *turns = "FLR"; // Straight left and right
const int rc[4][4] = {
{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // The change of North, Southeast and West coordinates
//strchr It's about finding ch stay dirs Subscript in , Use subscripts to indicate orientation and steering
int dir_id(char ch) { return strchr(dirs, ch) - dirs; }
int turn_id(char ch) { return strchr(turns, ch) - turns; }
node walk(const node &n, int turn); // Take a step in the maze
// Determine whether the coordinates are legal
bool legal(int row, int col) { return row >= 1 && row <= 9 && col >= 1 && col <= 9; }
int main()
{
string name;
while (cin >> name && name != "END")
{
cout << name << endl;
int row, col;
char ch;
cin >> row >> col >> ch;
node start(row, col, dir_id(ch)); // Starting point
cin >> row >> col;
node end(row, col, 0); // The end , Face casually
memset(maze, 0, sizeof(maze));
while (cin >> row && row != 0) // Maze data input
{
cin >> col;
string str;
while (cin >> str && str != "*")
for (int i = 1; i < str.size(); i++)
maze[row][col][dir_id(str[0])][turn_id(str[i])] = true;
// The four dimensions represent the position, direction and direction respectively , For true Is able to turn , On the contrary, we can't
}
memset(dis, -1, sizeof(dis));
node now = walk(start, 0); // Take a step from the starting point to the maze , Because the direction of the starting point is determined , This step also determines ,0 It means no turning
dis[now.row][now.col][now.dir] = 1; // Note that the distance between this position and the starting point is 1
queue<node> q;
q.push(now);
bool isout = false;
while (!q.empty()) //BFS
{
now = q.front();
q.pop();
if (now.row == end.row && now.col == end.col) // When you reach the destination, you will output the result
{
isout = true;
vector<node> vec;
while (dis[now.row][now.col][now.dir] != 1) // Path push back , Save to array
{ // Because nbefore The previous node information of the current node is saved
vec.push_back(now);
now = nbefore[now.row][now.col][now.dir];
}
vec.push_back(walk(start, 0));
vec.push_back(start);
// Format output
int count = 0;
for (int i = vec.size() - 1; i >= 0; i--)
{
if (++count % 10 == 1)
cout << ' ';
cout << " (" << vec[i].row << ',' << vec[i].col << ")";
if (count % 10 == 0)
cout << endl;
}
if (vec.size() % 10 != 0)
cout << endl;
break; // The current maze has been solved , sign out BFS Cycle
}
else // Otherwise, it will follow BFS
{
for (int i = 0; i < 3; i++) // Straight left and right
{
node gone = walk(now, i); // Suppose you take one step
// The first condition determines whether the steering is allowed , The second condition determines whether the coordinates are in the maze , The third coordinate determines whether the point has passed
if (maze[now.row][now.col][now.dir][i] && legal(gone.row, gone.col) && dis[gone.row][gone.col][gone.dir] == -1)
{
q.push(gone); // Join the queue for the next step BFS
// Record the distance , Pre record node
dis[gone.row][gone.col][gone.dir] = dis[now.row][now.col][now.dir] + 1;
nbefore[gone.row][gone.col][gone.dir] = now;
}
}
}
}
if (isout == false) // If no path result is output
cout << " No Solution Possible" << endl;
}
return 0;
}
node walk(const node &n, int turn)
{
int dir = n.dir;
switch (turn)
{
case 1:
dir = (dir + 3) % 4; // Turn left to update the direction dirs Previous , Anticlockwise
break;
case 2:
dir = (dir + 1) % 4; // Turn right to update the direction dirs The latter one , clockwise
}
return node(n.row + rc[dir][0], n.col + rc[dir][1], dir);
}边栏推荐
- Small sample learning data set
- CUDA compilation error
- JS handwriting depth clone array and object
- Compatible with Internet Explorer
- How to use the Magic pig system reinstallation master
- Get to know the drawing component of flutter - custompaint
- Five simple data types of JS
- CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
- Click to jump out and drag the pop-up window
- Apache+php uploading large files
猜你喜欢

Teach you to write non maintainable PHP code step by step
![[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]](/img/a1/f7a35a04e180e89d7f2fdbf89c1160.jpg)
[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]

Qdebug June 2022

great! Auto like, I use pyautogui!

固態硬盤開盤數據恢複的方法
![Bind simulation, key points of interpreting bind handwritten code [details]](/img/03/6aa300bb8b8342199aed5a819f3634.jpg)
Bind simulation, key points of interpreting bind handwritten code [details]

Laravel's little knowledge

Two hours to take you into the software testing industry (with a full set of software testing learning routes)

Wechat applet new version prompt update

buuctf(pwn)
随机推荐
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
How to download and use Xiaobai one click reload on the official website
Eyeshot Ultimate 2022 Crack By Xacker
Svg code snippet of loading animation
How to choose the years of investment in financial products?
Drag modal box
Could not find “store“ in the context of “Connect(homePage)
There is 404 in the laravel visit, except the home page is redirected; Index php
Laravel Aurora push
Student achievement management system based on SSH
H5 canvas drawing circle drawing fillet [detailed explanation]
parallel recovery slave next change & parallel recovery push change
Working principle of asemi three-phase rectifier bridge
2021-04-02
H5 native player [learn video]
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Database low-end SQL query statement fragment
buuctf web
Introduction to the hardest core PWN in the whole network_ Graphic analysis