pat-top 1012. Greedy Snake (35)
来源:互联网 发布:linux怎么执行命令 编辑:程序博客网 时间:2024/06/12 01:12
https://www.patest.cn/contests/pat-t-practise/1012
参考了http://blog.csdn.net/jtjy568805874/article/details/52517821
#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int maxn = 17;int board[maxn][maxn]; //1 and n is the wallint n, k;const int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };int dfs(int x, int y, int cnt) {int i,nx,ny,ans=0;bool deadEnd = true;for (i = 0; i < 4;i++){int steps = 1;while (board[x + dir[i][0] * steps][y + dir[i][1] * steps] != 2) steps++;steps--;if (steps == 0) continue;deadEnd = false;for (int j = 0; j <= steps; j++) board[x + dir[i][0] * j][y + dir[i][1] * j] = 2;ans = max(ans, dfs(x + dir[i][0] * steps, y + dir[i][1] * steps, cnt + steps));for (int j = 0; j <= steps; j++) board[x + dir[i][0] * j][y + dir[i][1] * j] = 0;}if (deadEnd) {//printf("Dead end: %d %d\n",x,y);return cnt;}return ans;}int main(){int a, b, ans = 0, nAns = 0;scanf("%d %d", &n, &k);memset(board, 0, sizeof(board));//build the wallfor (int i = 1; i <= n; i++)board[1][i] = board[n][i] = board[i][1] = board[i][n] = 2;for (int i = 0; i < k; i++){scanf("%d %d", &a, &b);board[a][b] = 2;}for (int i = 2; i <= n - 1; i++)for (int j = 2; j <= n - 1; j++) {if (board[i][j] == 2) continue;int res = dfs(i, j, 1);if (ans == res) nAns++;else if (ans < res) { ans = res; nAns = 1; }}printf("%d %d\n",(n-2)*(n-2)-k-ans,nAns);int newAns = 0 , nNewAns = 0;for (int i = 2; i <= n; i++)for (int j = 2; j <= n; j++) {if (board[i][j] == 2) continue;board[i][j] = 2;int res = 0;for (int ii = 2; ii <= n; ii++)for (int jj = 2; jj <= n; jj++)if (board[ii][jj] != 2) {res = max(res, dfs(ii, jj, 1));}board[i][j] = 0;if (newAns == res) nNewAns++;else if (newAns < res) { newAns = res; nNewAns = 1; }}if(newAns>=ans)printf("%d %d\n",(n-2)*(n-2)-k-1-newAns,nNewAns) ;else printf("-1\n"); return 0;}
0 0
- pat-top 1012. Greedy Snake (35)
- PAT (Top Level) Practise 1012Greedy Snake (35)
- 1012. Greedy Snake (35)解题报告
- hpuoj 1668: Problem F Greedy Snake
- Greedy Snake In Java ----My First Project
- pat-top 1002. Business (35)
- A good program of the greedy snake game.
- SicilyOJ 11珠海赛重现 F.Greedy Snake(深搜)
- PAT (Top Level) Practise 1002Business (35)
- PAT-TOP 1010. Lehmer Code (35)
- pat-top 1003. Universal Travel Sites (35)
- pat-top 1005. Programming Pattern (35)
- pat-top 1008. Airline Routes (35)
- pat-top 1009. Triple Inversions (35)
- pat-top 1010. Lehmer Code (35)
- pat-top 1013. Image Segmentation (35)
- pat-top 1014. Circles of Friends (35)
- pat-top 1015. Letter-moving Game (35)
- Appium下载遇到的问题
- 正则表达式
- 51nod 1441 士兵的数字游戏【素数筛】
- 设计模式 外观模式 一键电影模式
- CSS3新增常用属性
- pat-top 1012. Greedy Snake (35)
- CRC校验算法
- angularjs官方教程 -- 环境部署
- Vue.js——vue-resource全攻略
- 网易短网址转换
- sort的升级
- OpenGL ES入门09-GLSL实现常见特效
- SSH命令,ls命令
- 详解Bitmap使用,以及BitmapDrawable和BitmapFactory