QUTOJ 飘飘和小石(简单搜索+暴力)

来源:互联网 发布:自动化营销软件 编辑:程序博客网 时间:2024/04/30 07:54

题目连接:http://115.28.203.224/problem.php?cid=1012&pid=2





解题思路:


如图所示,石凳是首尾相接的。从石凳1开始,按照题目要求走石凳。用暴力的方法,定义一个vis[]数组进行标记,如果遍历完发现存在没有走过的石凳,输出NO;否则YES。










代码实现:

#define _CRT_SECURE_NO_DEPRECATE#include<iostream>#include<stdio.h>#include<cstdio>#include<string.h>#include<cstring>#include<string>#include<queue>#include<malloc.h>//头文件包含malloc函数,用来申请内存空间 #include<algorithm>#include<math.h>using namespace std;const int maxn = 10000 + 10;const int INF = 1e9;int vis[maxn];//判断路径int n, t;int main() {scanf("%d", &t);while (t--) {scanf("%d", &n);int z=0;int ans = 1;int flog = 0;vis[1] = 1;//如果走过,标为1for (int i = 0; i <= n*n; i++) {z = (i + ans) % n;//表示下标if (z == 0)z = n;vis[z] = 1;ans = z;}for (int i = 1; i <= n; i++) {if (vis[i] == 0) {//如果存在没有走过的石头flog = 1;break;//只要遇到第一个没有走过的石头就结束循环}}if (!flog)printf("YES\n");elseprintf("NO\n");}system("pause");return 0;}







0 0