Codeforces Beta Round #3

简介: 点击打开链接 A #include#include#include#include#includeusing namespace std;#define MAXN 500010#define N 10int ans...

点击打开链接


A

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;

#define MAXN 500010
#define N 10

int ans , end;
int sx , sy , ex , ey;
int vis[N][N];
int dir[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
char mp[N][N];
struct Point{
   int x;
   int y;
   int pre;
   int d;
   int step;
}p[MAXN];

void init(){
    strcpy(mp[0] , "U");
    strcpy(mp[1] , "RU");
    strcpy(mp[2] , "R");
    strcpy(mp[3] , "RD");
    strcpy(mp[4] , "D");
    strcpy(mp[5] , "LD");
    strcpy(mp[6] , "L");
    strcpy(mp[7] , "LU");
}

void BFS(){
    int front , rear;
    front = 0 , rear = 1;

    memset(vis , 0 , sizeof(vis));
    p[front].x = sx , p[front].y = sy;
    p[front].pre = 0 , p[front].step = 0;
    vis[sx][sy] = 1;

    while(front < rear){
       Point tmp = p[front];/*取出队列的头然后头指针加加*/
       if(tmp.x == ex && tmp.y == ey){
          end = front;
          ans = tmp.step;  
          break;
       }
       front++;
       for(int i = 0 ; i < 8 ; i++){
          if(tmp.x+dir[i][0] <= 0 || tmp.x+dir[i][0] > 8)
            continue;
          if(tmp.y+dir[i][1] <= 0 || tmp.y+dir[i][1] > 8)
            continue;
          if(vis[tmp.x+dir[i][0]][tmp.y+dir[i][1]])
            continue;
          vis[tmp.x+dir[i][0]][tmp.y+dir[i][1]] = 1;
          p[rear].x = tmp.x+dir[i][0] , p[rear].y = tmp.y+dir[i][1];
          p[rear].pre = front-1;
          p[rear].d = i;
          p[rear++].step = tmp.step+1;
       }
    }
}

void output(int x){
    if(x == 0)
      return;
    output(p[x].pre);
    printf("%s\n" , mp[p[x].d]);
}

int main(){
    string str1 , str2;
    init();
    while(cin>>str1>>str2){
       ans = 0;
       sx = str1[0]-'a'+1 , sy = str1[1]-'0';
       ex = str2[0]-'a'+1 , ey = str2[1]-'0';
       
       BFS();
       printf("%d\n" , ans);
       output(end);
    }
    return 0;
}




目录
相关文章
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output ...
1227 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output...
1273 0
|
人工智能
Codeforces Beta Round #2 A,B,C
A. Winner time limit per test:1 second memory limit per test:64 megabytes input:standard input output:standard output ...
1203 0
|
Perl
Codeforces Beta Round #1 A,B,C
A. Theatre Square time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard ...
849 0
|
人工智能 vr&ar
Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
                              B. Rebranding The name of one small but proud corporation consists of n lowercase English letters.
925 0
容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
  Lengthening Sticks Problem's Link:  http://codeforces.com/contest/571/problem/A   Mean:  给出a,b,c,l,要求a+x,b+y,c+z构成三角形,x...
902 0
Codeforces Beta Round #1
点击打开链接 A #include #include using namespace std; long long n , m , a; int main(){ long long sum; while(cin>>...
902 0