CodeForces 676D代码 哪里有问题呢?

题目:

http://codeforces.com/problemset/problem/676/D

code:

#include <stdio.h>
#define MAXN 1001
#define LEFT 0
#define TOP 1
#define RIGHT 2
#define BOTTOM 3
struct block{
    //left,top,right,bottom
    int doors[4];
    int doors_num;
    int rotats;
    int minutes;
};
struct block labyrinth[MAXN][MAXN];
char visited[MAXN][MAXN];

struct node{
    int x;
    int y;
};
int top;
struct node path[MAXN*MAXN];
int N,M;
int xt,yt,xm,ym;
int min_minutes = 0x0FFFFFFF;
void init_doors(int x,int y,char c)
{
    int i;
    for (i=0;i<4;i++)
    {
        labyrinth[x][y].doors[i] = 0;
    }
    labyrinth[x][y].doors_num =0;
    labyrinth[x][y].rotats = 0;
    labyrinth[x][y].minutes = 0x0FFFFFFF;
    switch(c)
    {
    case ‘+‘:
        for (i=0;i<4;i++)
        {
            labyrinth[x][y].doors[i] = 1;
        }
        labyrinth[x][y].doors_num = 4;
        labyrinth[x][y].rotats = 0;
        break;
    case ‘-‘:
        labyrinth[x][y].doors[LEFT] = 1;
        labyrinth[x][y].doors[RIGHT] =1;
        labyrinth[x][y].doors_num = 2;
        break;
    case ‘|‘:
        labyrinth[x][y].doors[TOP] = 1;
        labyrinth[x][y].doors[BOTTOM] =1;
        labyrinth[x][y].doors_num = 2;
        break;
    case ‘^‘:
        labyrinth[x][y].doors[TOP] = 1;
        labyrinth[x][y].doors_num = 1;
        break;
    case ‘>‘:
        labyrinth[x][y].doors[RIGHT] = 1;
        labyrinth[x][y].doors_num = 1;
        break;
    case ‘<‘:
        labyrinth[x][y].doors[LEFT] = 1;
        labyrinth[x][y].doors_num = 1;
        break;
    case ‘V‘:
        labyrinth[x][y].doors[BOTTOM] = 1;
        labyrinth[x][y].doors_num = 1;
        labyrinth[x][y].rotats = 0;
        break;
    case ‘L‘:
        labyrinth[x][y].doors[TOP] = 1;
        labyrinth[x][y].doors[BOTTOM] =1;
        labyrinth[x][y].doors[RIGHT] = 1;
        labyrinth[x][y].doors_num =3;
        labyrinth[x][y].rotats = 0;
        break;
    case ‘R‘:
        labyrinth[x][y].doors[TOP] = 1;
        labyrinth[x][y].doors[BOTTOM] =1;
        labyrinth[x][y].doors[LEFT] = 1;
        labyrinth[x][y].doors_num = 3;
        labyrinth[x][y].rotats = 0;
        break;
    case ‘U‘:
        labyrinth[x][y].doors[RIGHT] = 1;
        labyrinth[x][y].doors[BOTTOM] =1;
        labyrinth[x][y].doors[LEFT] = 1;
        labyrinth[x][y].doors_num = 3;
        break;
    case ‘D‘:
        labyrinth[x][y].doors[TOP] = 1;
        labyrinth[x][y].doors[RIGHT] =1;
        labyrinth[x][y].doors[LEFT] = 1;
        labyrinth[x][y].doors_num = 3;
        break;
    }
}
void push(int i,int j)
{
    top++;
    path[top].x = i;
    path[top].y = j;

}
struct node pop()
{
    top--;
    return path[top +1];
}
int get_door(int x,int y,int direction,int roates)
{
    int t = roates % 4;
    int index = direction - t;
    if (index < 0)
    {
        index += 4;
    }

    return labyrinth[x][y].doors[index];

}
void process_state(int x,int y,int cur_rotates,int cur_time)
{
    int dir_y[4] = {-1,0,1,0};
    int dir_x[4] = { 0,-1,0,1};
    int i,nx,ny;
    for (i=0;i<4;i++)
    {
        nx = x+dir_x[i];
        ny = y+dir_y[i];
        if (nx<1 || nx >N)
        {
            continue;
        }
        if (ny<1 || ny>M)
        {
            continue;
        }
        if (get_door(x,y,i,cur_rotates)&& get_door(nx,ny,(i+2)%4,cur_rotates))
        {
            if (!visited[nx][ny])
            {
                visited[nx][ny] =1;
                labyrinth[nx][ny].minutes = cur_time+1;
                labyrinth[nx][ny].rotats = cur_rotates;
                if (nx == xm && ny == ym)
                {
                    if (min_minutes > cur_time+1)
                    {
                        min_minutes = cur_time+1;
                    }
                }else
                {

                    push(nx,ny);
                }

            }else
            {
                if (labyrinth[nx][ny].minutes >cur_time+1 )
                {
                    labyrinth[nx][ny].minutes = cur_time+1;
                    labyrinth[nx][ny].rotats = cur_rotates;
                    push(nx,ny);
                }
            }
        }

    }
}
int bfs()
{
    int x,y,cur_rotates,i,nx,ny,t,cur_time;

    struct node cur;
    top = -1;
    push(xt,yt);
    labyrinth[xt][yt].minutes = 0;
    visited[xt][yt] =1;

    while(top!= -1)
    {
        cur = pop();

        //left
        x = cur.x;
        y = cur.y;
        cur_rotates = labyrinth[x][y].rotats;
        cur_time = labyrinth[x][y].minutes;
        t = 0;
        while(t < 4)
        {
            if (cur_time + t < min_minutes)
            {
                process_state(x,y,cur_rotates+t,cur_time+t);
            }
            t++;

        }
    }
    return labyrinth[xm][ym].minutes;

}
int main()
{
    int i,j;
    char str[MAXN];    scanf("%d %d",&N,&M);
    for (i=1;i<=N;i++)
    {
        scanf("%s ",str);
        for (j=1;j<=M;j++)
        {

            init_doors(i,j,str[j-1]);
            visited[i][j] = 0;
        }
    }
    scanf("%d %d",&xt,&yt);
    scanf("%d %d",&xm,&ym);
    //check same logcation
    if (xt == xm && yt == ym)
    {
        printf("0");
        return 0;
    }
    //dfs(xt,yt,0);

    bfs();
    if(min_minutes == 0x0FFFFFFF)
        printf("-1");
    else
        printf("%d",min_minutes);

    return 0;
}
时间: 2024-10-08 15:10:45

CodeForces 676D代码 哪里有问题呢?的相关文章

codeforces -676D(Theseus and labyrinth)

1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <bitset> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <cmath> 10 #include <li

Codeforces Round #354 (Div. 2)

5/5 水了场CF,写个水水地题解~~ 题A CodeForces 676A 题意:给你一个排列,问你交换一次,最大最小位置最远是多少? 题解:暴力交换,暴力算,反正数据小.偷懒被hack更惨!! 1 /*zhen hao*/ 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define lson l, m, rt*2 6 #define rson m + 1, r, rt*2+1 7 #define xx first 8 #def

错误和问题解决的成本

问题描写叙述 错误 数据收集 根本原因 版本号   组件:数据修复           在一个实际成本组织中,(平均,先进先出,后进先出) 一个或更 多的下面情况可能发生: 1.导航到物料成本历史表单上的数量信息,与现有量表单的数量不匹配的记录 2. 一些物料前期已计成本的数量与前面的事务处理历史表单的数量不匹配 3. 全部的库存值报表与事务处理值报表不匹配 4. 存货层次成本更新表单的总数量与现有量数量表单不匹配(只在先进先出/后进先出) 5.这些症状的不论什么一个意味着 MMT-CQL不匹配

Educational Codeforces Round 50 (Rated for Div. 2)的A、B、C三题AC代码

A题链接:https://codeforces.com/contest/1036/problem/A A题AC代码: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <malloc.h> 6 #include <stdbool.h> 7 #include <ctype.h> 8 9

Codeforces 111B【看看自己和别人在代码能力上的差距!】

我的: #include<iostream> #include<cstring> using namespace std; int x[100001],y[100001],d[100001]; int i =1; int main() { memset(d,-1,sizeof(d)); int n; cin>>n; while(n--) { int ans = 0,m; cin>>x[i]>>y[i]; for(int j = 1; j*j &l

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Round #393 div2

A. Petr and a calendar(模拟) http://codeforces.com/problemset/problem/760/A 题意:已知2017年m月1日是星期d,求这个月日历有多少行. 算法:简单模拟或者直接用公式算. 代码: #include<bits/stdc++.h> using namespace std; int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() {