Game

poj 5523

这个题目属于事件分析,只要把主要的条理弄清了也就能写了,程序的结果无非4种:-1 0 1 2

首先要知道在n个数中选择任意两个s和t会有很多种,所以应该要减少复杂程度,其中答案属于2的情况最多,因此要先判断 -1 0 1的情况,剩下的就都属于2了

首先如果出口和入口重合的话,那就为-1,不过有一种特殊:n==0时应该是0

其余如果出口在1位置或者n位置,再或者出口和入口相邻应该是1

其余都为2(不可能还存在其他走不出来的情况)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int solve(int n,int s,int t)
{
    if(n==1) return 0;
    if(s==t) return -1;
    if((s==1&&t==n)||(s==n&&t==1)) return 0;
    if((s==1&&t!=n)||(s==n&&t!=1)||(abs(s-t)==1)) return 1;
    return 2;
}

int main()
{
    int n,s,t;
    while(cin>>n>>s>>t)
        printf("%d
",solve(n,s,t));
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zsyacm666666/p/4927684.html