列表

详情


NC24393. perimeter

描述

Farmer John has arranged N hay bales (1 <= N <= 10,000) in the middle of one of his fields. If we think of the field as a 100 x 100 grid of 1 x 1 square cells, each hay bale occupies exactly one of these cells (no two hay bales occupy the same cell, of course). 
FJ notices that his hay bales all form one large connected region, meaning that starting from any bale, one can reach any other bale by taking a series of steps either north, south, east, or west onto directly adjacent bales. The connected region of hay bales may however contain "holes" -- empty regions that are completely surrounded by hay bales.  
Please help FJ determine the perimeter of the region formed by his hay bales. Note that holes do not contribute to the perimeter.

输入描述

* Line 1: The number of hay bales, N.

* Lines 2..1+N: Each line contains the (x,y) location of a single hay
bale, where x and y are integers both in the range 1..100.
Position (1,1) is the lower-left cell in FJ's field, and
position (100,100) is the upper-right cell.

输出描述

* Line 1: The perimeter of the connected region of hay bales.

示例1

输入:

8
5 3
5 4
8 4
5 5
6 3
7 3
7 4
6 5

输出:

14

说明:

INPUT DETAILS:
The connected region consisting of hay bales looks like this:
XX
X XX
XXX

OUTPUT DETAILS:
The length of the perimeter of the connected region is 14 (for example, the
left side of the region contributes a length of 3 to this total). Observe
that the hole in the middle does not contribute to this number.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java(javac 1.8) 解法, 执行用时: 395ms, 内存消耗: 31384K, 提交时间: 2020-05-30 11:17:33

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int N = sc.nextInt(), a = 0, b = 0;
			boolean[][] grid = new boolean[100][100];
			for (int i = 0; i < N; i++) {
				grid[sc.nextInt() - 1][sc.nextInt() - 1] = true;
			}
			for (int i = 0; i < 100; i++) {
				for (int j = 0; j < 100; j++) {
					if (dfs(i, j, grid)) {
						dfs(grid, i, j);
					}
				}
			}
			for (int i = 0; i < 100; i++) {
				for (int j = 0; j < 100; j++) {
					if (grid[i][j]) {
						a++;
						b += (i < 99 && grid[i + 1][j] ? 1 : 0) + (j < 99 && grid[i][j + 1] ? 1 : 0);
					}
				}
			}
			System.out.println(4 * a - 2 * b);
		}
	}

	private static boolean dfs(int i, int j, boolean[][] grid) {
		if (i < 0 || i > 99 || j < 0 || j > 99) {
			return false;
		} else if (grid[i][j]) {
			return true;
		}
		grid[i][j] = true;
		boolean result = dfs(i - 1, j, grid) && dfs(i, j - 1, grid) && dfs(i, j + 1, grid) && dfs(i + 1, j, grid);
		grid[i][j] = false;
		return result;
	}

	private static void dfs(boolean[][] grid, int i, int j) {
		if (i < 0 || i > 99 || j < 0 || j > 99) {
			return;
		} else if (!grid[i][j]) {
			grid[i][j] = true;
			dfs(grid, i - 1, j);
			dfs(grid, i, j - 1);
			dfs(grid, i, j + 1);
			dfs(grid, i + 1, j);
		}
	}
}

C++14(g++5.4) 解法, 执行用时: 9ms, 内存消耗: 992K, 提交时间: 2020-06-22 23:52:56

#include<bits/stdc++.h>
using namespace std;
set< pair<int,int> > st;
set< pair<int, int> > vis;
int n;
int ans = 0;
int dx[]={0,0,-1,1,1,1,-1,-1};
int dy[]={1,-1,0,0,1,-1,1,-1};

bool check(int x, int y)
{
    for(int i = 0; i < 8; i++) if(st.count(make_pair(x+dx[i], y+dy[i]))) return 1;
    return 0;
}

void dfs(int x, int y)
{
    if(st.count(make_pair(x, y)) )
    {
        ans++;
        return;
    }
    if(vis.count(make_pair(x, y)) )
    {
        return;
    }
    if(!check(x, y)) return;
    vis.emplace(make_pair(x, y));
    for(int i = 0; i < 4; i++) dfs(x+dx[i], y+dy[i]);
}
int main()
{
    scanf("%d",&n);
    for(int i = 0, x, y ; i < n; i++) scanf("%d %d",&x,&y), st.emplace(make_pair(x, y));
    auto p = *st.begin();
    dfs(p.first-1, p.second);
    printf("%d\n",ans);
}

C++11(clang++ 3.9) 解法, 执行用时: 16ms, 内存消耗: 864K, 提交时间: 2020-05-30 10:47:37

#include<bits/stdc++.h>
#define Max 1000000
using namespace std;
int n,y,x,i,sy=Max,sx=Max,ans;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
map<int,map<int,int> >mat;
int main(){
	cin>>n;
	for(int j=1;j<=n;j++){
		cin>>x>>y;
		mat[y][x]=1;
		if(y<sy||(y==sy&&x<sx)) sy=y,sx=x;
	}
	x=sx;y=sy;
	do{
		x+=dx[i];y+=dy[i];ans++;
		for(i=(i+3)%4; ;i=(i+1)%4)
			if((i==0&&mat[y][x])||(i==1&&mat[y-1][x])||(i==2&&mat[y-1][x-1])||(i==3&&mat[y][x-1])) break;//如果我们已经搜索到的话,跳出即可。
	}while(x!=sx||y!=sy);
	cout<<ans<<endl;
	return 0;
}

上一题