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

#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. Linux中配置JDK的环境变量

    一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车, ...

  2. 2019冬季PAT甲级第一题

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ][]; ][]; ]; string ...

  3. sublime-text3 安装 emmet 插件

    下载sublime,http://www.sublimetext.com/ 安装package control :https://packagecontrol.io/ins... 这个地址需要翻墙,访 ...

  4. 如何在Linux中显示和设置主机名(适用ubantu、centos等版本)

    随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的操作手册来 ...

  5. MyBatis-Spring整合之方式3

    通过注解整合 1.在UserDao同级目录下新建接口UserMapper,内容如下: public interface UserMapper { @Select("select * from ...

  6. idea项目更改git地址

    第一步:idea打开项目,菜单栏找VCS - Git - Remotes 点进去,弹出对话框,选中,点击编辑 弹出编辑框,更改地址,点击ok 弹出输入账号密码编辑框,输入自己的账号密码,点击确认 完成 ...

  7. 【代码学习】PYTHON 线程

    一.使用threading模块多线程执行 可以明显看出使用了多线程并发的操作,花费时间要短很多 创建好的线程,需要调用start()方法来启动 #coding=utf-8 import threadi ...

  8. eclipse好用的快捷键

    eclipse一些增加工作效率的快捷键(部分自己添加) eclipse如何修改快捷键 1. shift + enter | ctrl + shift + enter 在下/上方增加空行 2. ctrl ...

  9. 让 typora和word一样好用

    让 typora和word一样好用  :https://github.com/itcastWsy/typora_copy_images typora是一款支持实时预览的markdown编辑器,作者在使 ...

  10. mysql 远程连接不上,bind-address参数配置要求,以及怎么去使得mysql能够允许远程的客户端访问

    刚安装了MySQL服务器,使用远程管理工具总是连接不上,因为知道mysql的默认端口是3306,于是使用telnet连接这个端口,(从这里可以学到telnet是可以这样用的) telnet 192.1 ...