Codeforces Beta Round #3

点击打开链接

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;
}
时间: 2024-10-24 22:27:24

Codeforces Beta Round #3的相关文章

Codeforces Beta Round #10

点击打开链接cf#10 A 思路:模拟 分析: 1 题目要求找到总共的电脑的消耗.题目明确指出在n个时间段之内电脑都是属于第一种状态,并且如果不是第一种状态只要移动鼠标或按键盘马上变为第一种状态. 2 题目还指出每一组输入都保证L<R,并且Ri-1<Li.那么我们只要每输入一个就处理一个即可. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>

Codeforces Beta Round #9 (Div. 2 Only)

点击打开链接cf#9 A 思路:求gcd然后化简 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int gcd(int a ,int b){ return b == 0 ? a : gcd(b , a%b); } int main(){ int a , b; while(scanf("%d%d"

Codeforces Beta Round #11

点击打开链接 A #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int MAXN = 2010; int n , d; int num[MAXN]; int main(){ while(scanf("%d%d" , &n , &d) != EOF){ for(int i

Codeforces Beta Round #8

点击打开链接 A 思路:正反字符串各自判断一次是否有对应的两个子串 分析: 1 题目给定一个字符串str,然后给定两个不同时间段内看到的子串s1 , s2,判断是哪一种情况. 2 我们知道两个时间段内那么看到的字符串是有间隔的,那么如果我们怎么知道是否是"向前""向后""都有""没有"这四种答案中的哪一种呢?其实我们知道如果给定的两个子串都是给定的字符串str的子串,那么我们就必须正反都判断是否都能按照顺序看到s1 和 s2.

Codeforces Beta Round #12 (Div 2 Only)

点击打开链接 A #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char str[3][3]; bool isOk(){ for(int i = 0 ; i < 3 ; i++){ for(int j = 0 ; j < 3 ; j++){ if(str[i][j] != str[2-i][2-j]) r

Codeforces Beta Round #7

点击打开链接 A #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 8 char G[N][N]; int main(){ while(scanf("%s" , G[0]) != EOF){ for(int i = 1 ; i < 8 ; i++) scanf("%

Codeforces Beta Round #6 (Div. 2 Only)

点击打开链接 A #include<iostream> #include<cstdio> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int l[4]; int main(){ while(scanf("%d%d%d%d" , &l[0] , &l[1] , &l[2] , &l[3])

Codeforces Beta Round #1

点击打开链接 A #include<iostream> #include<cstdio> using namespace std; long long n , m , a; int main(){ long long sum; while(cin>>n>>m>>a){ long long x , y; x = n%a == 0 ? n/a : n/a+1; y = m%a == 0 ? m/a : m/a+1; sum = x*y; cout&l

Codeforces Beta Round #2

点击打开链接 A /* 思路:模拟 分析: 1 题目要求的在游戏结束后最大的点的值的玩家,或者具有相同点值的情况下最早出现 */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; #define MAXN 1010 #define N 50 int n , pos; int vis