C++——【USACO 5.2.1】——Snail Trails

44 篇文章 0 订阅

Snail Trails
All Ireland Contest

Sally Snail likes to stroll on a N x N square grid (1 <n <= 120). She always starts in the upper left corner of the grid. The grid has empty squares (denoted below by `.') and a number (B) of barriers (denoted below by `#'). Here is a depiction of a grid including a demonstration of the grid labelling algorithm:

          A B C D E F G H
        1 S . . . . . # .
        2 . . . . # . . .
        3 . . . . . . . .
        4 . . . . . . . .
        5 . . . . . # . .
        6 # . . . . . . .
        7 . . . . . . . .
        8 . . . . . . . .

Sally travels vertically (up or down) or horizontally (left or right). Sally can travel either down or right from her starting location, which is always A1.

Sally travels as long as she can in her chosen direction. She stops and turns 90 degrees whenever she encounters the edge of the board or one of the barriers. She can not leave the grid or enter a space with a barrier. Additionally, Sally can not re-cross any square she has already traversed. She stops her traversal altogether any time she can no longer make a move.

Here is one sample traversal on the sample grid above:

          A B C D E F G H
        1 S---------+ # .
        2 . . . . # | . .
        3 . . . . . | . .
        4 . . . . . +---+
        5 . . . . . # . |
        6 # . . . . . . |
        7 +-----------+ |
        8 +-------------+

Sally traversed right, down, right, down, left, up, and right. She could not continue since she encountered a square already visited. Things might have gone differently if she had chosen to turn back toward our left when she encountered the barrier at F5.

Your task is to determine and print the largest possible number of squares that Sally can visit if she chooses her turns wisely. Be sure to count square A1 as one of the visited squares.

PROGRAM NAME: snail

INPUT FORMAT

The first line of the input has N, the dimension of the square, and B, the number of barriers (1 <= B <= 200). The subsequent B lines contain the locations of the barriers. The sample input file below describes the sample grid above. The sample output file below is supposed to describe the traversal shown above. Note that when N > 26 then the input file can not specify barriers to the right of column Z.

SAMPLE INPUT (file snail.in)

8 4
E2
A6
G1
F5

OUTPUT FORMAT

The output file should consist of exactly one line, the largest possible number of squares that Sally can visit.

SAMPLE OUTPUT (file snail.out)

33
Using this traversal:
          A B C D E F G H
        1 S . . . . . # .
        2 | . . . # . . .
        3 | . . . +-----+
        4 | . . . | . . |
        5 +-------+ # . |
        6 # . . . . . . |
        7 +------------ |
        8 +-------------+


蜗牛轨迹


Sally Snail喜欢在N * N 的正方形的网格上漫步(1 <= N <= 120)。她总是从格子的左上角开始。网格中有空的正方形是 ' . ' 或是一个数字(B)标记的屏障( ' # ')。以下是对网格的描述,其中包括网格标记算法的演示:

   A B C D E F G H
 1 S . . . . . # .
 2 . . . . # . . .
 3 . . . . . . . .
 4 . . . . . . . .
 5 . . . . . # . .
 6 # . . . . . . .
 7 . . . . . . . .
 8 . . . . . . . .

萨莉垂直地(上或下)或水平地移动(左或右)。莎莉从她的起点出发,起点一直都是A 1。
莎莉在她选定的方向上走得越远越好。当她遇到网格的边缘或其中一个障碍时,她就会停下来,转90度。她不能离开网格,也不能进入一个有障碍的空间。此外,莎莉不能再穿过她已经走过的任何地方。当她不能再移动时,她就完全停止了她的遍历。
下面是上面示例网格的一个示例遍历:
  A B C D E F G H
1 S---------+ # .
2 . . . . # | . .
3 . . . . . | . .
4 . . . . . +---+
5 . . . . . # . |
6 # . . . . . . |
7 +-----------+ |
8 +-------------+

莎莉穿过右、下、右、下、左、右、右。自从她遇到一个已经参观过的地方后,她就不能再继续下去了。如果她在F 5遇到障碍时,她选择转向左边,情况可能会有所不同。
你的任务是确定并打印出如果她选择明智的话,莎莉可以访问的最大数量的方块。一定要把方块A 1作为访问过的方块之一。
项目名称: snail
输入格式
输入的第一行有N个,正方形的长度,和B,障碍的数目(1 <= B <= 200)。随后的B行包含了这些障碍的位置。下面的示例输入文件描述了上面的示例网格。下面的示例输出文件应该描述上面所示的遍历。注意,当N大于26时,输入文件不能指定列z的右边的障碍。
示例输入(文件snail.in)
8 4
E 2
A 6
G 1
F 5
输出格式
输出文件应该由一行组成,这是萨利可以访问的最多可能的方块。
样例输出(文件snail.out)
33
使用这个遍历:
          A B C D E F G H
        1 S . . . . . # .
        2 | . . . # . . .
        3 | . . . +-----+
        4 | . . . | . . |
        5 +-------+ # . |
        6 # . . . . . . |
        7 +------------ |
        8 +-------------+



/*
ID : mcdonne1
LANG : C++
TASK : snail
*/

#include <iostream>
#include <fstream>
#include <bitset>
#include <cstring>
#define fail dx < 1  or  dy < 1  or  dx > n  or  dy > n  or  c[dx][dy] == off
#define pass dx >= 1 and dy >= 1 and dx <= n and dy <= n and c[dx][dy] == Free

using namespace std;

enum e{Free, off};
int n, m, b, ans;
char ch;
bitset <128> c[128], v[128];

const int fx[4] = {-1, 0, 0, 1};
const int fy[4] = {0, -1, 1, 0};

void bfs (int x, int y, int face, int step) {
	if (v[x][y] == true) return;
	v[x][y] = true;
	ans = max(ans, step);
	int dx = x + fx[face];
	int dy = y + fy[face];
	if (fail)
		if (face == 0 or face == 3) {
			dx = x + fx[1];
			dy = y + fy[1];
			if (pass) bfs (dx, dy, 1, step + 1);
			dx = x + fx[2];
			dy = y + fy[2];
			if (pass) bfs (dx, dy, 2, step + 1);
		} else {
			dx = x + fx[0];
			dy = y + fy[0];
			if (pass) bfs (dx, dy, 0, step + 1);
			dx = x + fx[3];
			dy = y + fy[3];
			if (pass) bfs (dx, dy, 3, step + 1);
		}
	else bfs (dx, dy, face, step + 1);
	v[x][y] = false;
}

int main () {
	freopen ("snail.in", "r", stdin);
	freopen ("snail.out", "w", stdout);
	scanf ("%d %d\n", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf ("%c %d\n", &ch, &b);
		c[ch - 'A' + 1][b] = off;
	}
	for (int i = 2; i <= 3; i++) {
		memset (v, 0, sizeof(v));
		bfs (1, 1, i, 1);
	}
	printf ("%d\n", ans);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值