zoukankan      html  css  js  c++  java
  • P1518 两只塔姆沃斯牛 The Tamworth Two

    模拟

    题目链接:https://www.luogu.org/problem/show?pid=1518

    非常简单的模拟,刚开始我想的是bfs,

    理解成了相遇的最小步数。

    然后……调了一上午,QAQQ

    重新看题,发现理解错了……(我到底在想什么)

    重点是……题目都告诉你怎么写了!!!

    (截自洛谷)

    我还能不能再zz一点……QAQQ

    不扯了,伤心……

    注意判重需要确定整个状态

    包括两个坐标和各自的方向,

    如果走到相同的状态,则不可能相遇

    其他的还真没什么好说的……伤心QAQ

    Codes:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<queue>
     6 
     7 using namespace std;
     8 
     9 const int N = 10 + 10;
    10 char mp[N][N];
    11 bool vis[N][N][N][N][N][N];
    12 int xx[] = {0,-1,0,1,0},yy[] = {0,0,1,0,-1};
    13 
    14 bool check(int x,int y){
    15     if(x > 10 || x < 1 || y < 1 || y > 10) return false;    //越界 
    16     if(mp[x][y] == '*') return false;    //障碍物 
    17     return true;
    18 }
    19 int work(int x,int y,int lx,int ly){
    20     int f1,f2;
    21     f1 = f2 = 1;
    22     int step = 0;//minutes 
    23     while(1){
    24         if(check(x + xx[f1],y + yy[f1])) //如果能走,继续原来的方向 
    25             x += xx[f1],y += yy[f1];
    26         else                         //否则用这一分钟转变方向 
    27             f1 = f1 % 4 + 1;
    28         if(check(lx + xx[f2],ly + yy[f2])) 
    29             lx += xx[f2],ly += yy[f2];
    30         else 
    31             f2 = f2 % 4 + 1;
    32         step ++;
    33         if(vis[x][y][lx][ly][f1][f2]) //走到相同状态则不可能相遇 
    34             return 0;
    35         vis[x][y][lx][ly][f1][f2] = 1;
    36         if(x == lx && y == ly) //相遇 
    37             return step;
    38     }
    39     return 0;
    40 }
    41 int main(){
    42     int lx,ly,x,y;
    43     for(int i = 1;i <= 10;++ i){
    44         scanf("%s",mp[i] + 1); //注意不能写 &mp[i] + 1 
    45         for(int j = 1;mp[i][j];++ j){
    46             if(mp[i][j] == 'F') 
    47                 lx = i,
    48                 ly = j;
    49             if(mp[i][j] == 'C')
    50                 x = i,
    51                 y = j;
    52         }
    53     }
    54     if(x == lx && y == ly) {
    55         cout << "0" << '
    ';
    56         return 0;
    57     }
    58     int ans = work(x,y,lx,ly);
    59     cout << ans << '
    ';
    60     return 0;
    61 }

    MAS:

    判重注意确定一种状态。

    看错题这种错误还是少犯,认真读题啊!!

  • 相关阅读:
    angular模板
    Growth: 全栈增长工程师指南
    全栈增长工程师实战
    vue 快速搭建项目 iview
    ng-style
    教程视频链接
    内置对象
    对象—封装、继承
    对象—构造函数
    函数-理论
  • 原文地址:https://www.cnblogs.com/Loizbq/p/7761617.html
Copyright © 2011-2022 走看看