直接暴力枚举,注意每次深搜完状态的还原~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int visit[maxn][maxn];
int N,M,x,y;
int cnt;
int maxcnt;
int X[]={,,-,};
int Y[]={,,,-};
int judge (int x,int y) {
if (x<||x>N||y<||y>N) return ;
if (visit[x][y]) return ;
return ;
}
void dfs (int x,int y,int pre) {
visit[x][y]=;
cnt++;
int tx=x+X[pre];
int ty=y+Y[pre];
if (judge(tx,ty)) dfs (tx,ty,pre);
else {
int flag=;
for (int i=;i<;i++) {
tx=x+X[i];
ty=y+Y[i];
if (judge(tx,ty)) flag++,dfs(tx,ty,i);
}
if (flag==) maxcnt=max(maxcnt,cnt);
}
visit[x][y]=;
cnt--;
}
int main () {
scanf ("%d %d",&N,&M);
N-=;
for (int i=;i<M;i++) {
scanf ("%d %d",&x,&y);
x--;
y--;
visit[x][y]=;
}
int ans=;
int num=;
for (int i=;i<=N;i++) {
for (int j=;j<=N;j++) {
if (judge(i,j)) {
maxcnt=;
dfs (i,j,);
dfs (i,j,);
dfs (i,j,);
dfs (i,j,);
if (maxcnt>ans) {
ans=maxcnt;
num=;
}
else if (maxcnt==ans) {
num++;
}
}
}
}
printf ("%d %d\n",N*N-ans-M,num);
num=;
int ans1=;
for (int i=;i<=N;i++) {
for (int j=;j<=N;j++) {
if (visit[i][j]) continue;
visit[i][j]=;
maxcnt=;
for (int k=;k<=N;k++) {
for (int w=;w<=N;w++) {
if (judge(k,w)) {
dfs (k,w,);
dfs (k,w,);
dfs (k,w,);
dfs (k,w,);
}
}
}
if (maxcnt>ans1) {
ans1=maxcnt;
num=;
}
else if (maxcnt==ans1) {
num++;
}
visit[i][j]=;
}
}
if (ans1<ans) printf ("-1");
else printf ("%d %d\n",N*N-M-ans1-,num);
return ;
}

PAT T1012 Greedy Snake的更多相关文章

  1. Pascal小游戏 贪吃蛇

    一段未完成的Pascal贪吃蛇 说这段代码未完成其实是没有源代码格式化,FP中一行最多只有255字符宽. uses crt; const screenwidth=50; screenheight=24 ...

  2. 2019秋季PAT甲级_C++题解

    2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...

  3. PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】

    7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...

  4. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  5. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...

  6. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  7. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  8. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  9. PAT 1041. 考试座位号(15)

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

随机推荐

  1. 【Hibernate 懒加载】debug模式懒加载获取空数据

    <many-to-one name="department" class="k.domain.Department" column="depar ...

  2. C语言程序的错误和警告

    一段C语言代码,在编译.链接和运行的各个阶段都可能会出现问题.编译器只能检查编译和链接阶段出现的问题,而可执行程序已经脱离了编译器,运行阶段出现问题编译器是无能为力的. 如果我们编写的代码正确,运行时 ...

  3. CSS学习(5)更多的选择器

    1.通配符选择器 * 表示选中所有元素 *{color:red;} 2.属性选择器  根据属性名和属性值选中元素 https://developer.mozilla.org/zh-CN/docs/We ...

  4. ACM的探索之Everything Is Generated In Equal Probability(这真的是很有趣的话语丫!)

    ---------------------------------------步履不停,奋勇前进! ------------------------难度真的是蛮大丫!后序补充!

  5. .NET core 部署到Docker +Docker Protainer管理实现

    .NET core 部署到Docker +Docker Protainer管理实现 上次说到将.net core的应用程序发布到Linux中(https://www.cnblogs.com/Super ...

  6. jmeter实现IP欺骗

    用jmeter模拟多个IP同时向一个目标发送请求 1.IP地址参数化 在csv文件中编辑参数化IP地址列表,参数化的IP需在同一个局域网,子网掩码相同(比如和客户端本机同一网段),如下 将csv列表中 ...

  7. php中判断shell_exec执行结果

    $shell = "wget -O despath sourcepath && echo 'success' "; $shellExec = shell_exec( ...

  8. python3将json数据转换到excel中

    #!/usr/bin/env python# coding=utf-8# json转换为excel import xlrdimport jsonimport osfrom openpyxl impor ...

  9. ASP.NET Core搭建多层网站架构【8.1-使用ViewModel注解验证】

    2020/01/29, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[8.1-使用ViewModel注解验证] 使用V ...

  10. spring web 测试用例

    spring web 测试有三种方式 1. 自己初始化 MockMvc 2.依赖@springbootTest 它会帮你生产 webTestClient ,只需自己注入即可. 3.启动的时候,不加载整 ...