码迷,mamicode.com
首页 > 其他好文 > 详细

P3083 [USACO13OPEN]豪华游船Luxury River Cruise

时间:2018-09-05 21:38:23      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:ase   algorithm   begin   etc   using   sse   bool   ack   cpp   

瞎模拟.
模拟的时候顺便把每次指令完成后到达的点记下来, 假如之前经过说明遇到了循环, 直接mod就ok.
时间复杂度 \(O(NM)\)

#include <vector>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + 10;
const int MAXM = 5e2 + 10;
inline int read()
{
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

struct Node
{
    bool vis;
    int l, r;
}node[MAXN];

int N, M, K;
char dir[MAXM];

vector<int> pos;
int main()
{
    // freopen("p3083.in", "r", stdin);
    cin>>N>>M>>K;
    for(int i = 1; i <= N; i++){
        int l = read(), r = read();
        node[i].l = l, node[i].r = r;
    }
    for(int i = 0; i < M; i++)
        scanf(" %c", dir + i);

    int p = 1; bool flag = false;
    pos.push_back(1), node[1].vis = true;
    for(int i = 1; i <= K; i++){
        for(int j = 0; j < M; j++){
            if(dir[j] == 'L') p = node[p].l;
            else p = node[p].r;
        }
        if(node[p].vis) flag = true;
        node[p].vis = true; pos.push_back(p);
        if(flag) break;
    }
    if(!flag) cout<<*pos.rbegin()<<endl;
    else{
        K -= pos.size();
        pos.erase(pos.begin(), find(pos.begin(), pos.end(), *pos.rbegin()) + 1);
        cout<<pos[K % pos.size()]<<endl;
    }
    return 0;
}

P3083 [USACO13OPEN]豪华游船Luxury River Cruise

标签:ase   algorithm   begin   etc   using   sse   bool   ack   cpp   

原文地址:https://www.cnblogs.com/wsmrxc/p/9594041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!