zoukankan      html  css  js  c++  java
  • NOIP 普及组 2012 寻宝(思维???)

    传送门

    https://www.cnblogs.com/violet-acmer/p/9937201.html

    题解:

      一开始用暴力查找下一个要去的房间,超时了,emmmmm.......

      然后,就稍微优化了一下,具体看代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int MOD=20123;
     4 const int maxn=1e4+50;
     5 
     6 int N,M;
     7 int pos;//当前位置
     8 int id[110];//存储每一层 pos 房间之后的 key==1 的房间编号
     9 struct Node
    10 {
    11     int key;//0 表示没有,1 表示有
    12     int index;//指示牌上的数 字
    13     Node(int a=0,int b=0):key(a),index(b){};
    14 }Floor[maxn][110];
    15 
    16 int nextRoom(int nowF,int pos)
    17 {
    18     int x=0;
    19     int p=pos;
    20     do
    21     {
    22         if(Floor[nowF][p].key == 1)
    23             id[++x]=p;//按照逆时针顺序,将key == 1的房间编号加入到id[]中
    24         p=(p+1 == M ? 0:p+1);
    25     }while(p != pos);
    26 
    27     int mod=Floor[nowF][pos].index%x;//id[mod]即为所求的房间
    28     return mod == 0 ? id[x]:id[mod];//特别注意,当mod == 0时,所求房间为 id[x]
    29 }
    30 void Solve()
    31 {
    32     int nowF=1;
    33     int sum=0;
    34     while(nowF <= N)
    35     {
    36         sum=sum%MOD+Floor[nowF][pos].index;//根据题意求解密钥
    37         pos=nextRoom(nowF,pos);//查找下一个要去的房间
    38         nowF++;
    39     }
    40     printf("%d
    ",sum%MOD);
    41 }
    42 int main()
    43 {
    44     scanf("%d%d",&N,&M);
    45     for(int i=1;i <= N;++i)
    46     {
    47         for(int j=0;j < M;++j)
    48         {
    49             int a,b;
    50             scanf("%d%d",&a,&b);
    51             Floor[i][j]=Node(a,b);
    52         }
    53     }
    54     scanf("%d",&pos);
    55     Solve();
    56 }
    View Code

      踩到一个坑:

        当 mod == 0 时,说明满足要求的房间为id[x]而不是id[0]

      

  • 相关阅读:
    237.Delete Node in a Linked List
    235.Lowest Common Ancestor of a Binary Search Tree
    234.Palindrome Linked List
    232.Implement Queue using Stacks
    231.Power of Two
    226.Invert Binary Tree
    225.Implement Stack using Queues
    Vue概述
    Git分布式版本控制工具
    分布式RPC框架Apache Dubbo
  • 原文地址:https://www.cnblogs.com/violet-acmer/p/9937391.html
Copyright © 2011-2022 走看看