列表

详情


NC24446. [USACO 2014 Dec B]Crosswords

描述

Like all cows, Bessie the cow likes to solve crossword puzzles.
Unfortunately, her sister Elsie has spilled milk all over her book of
crosswords, smearing the text and making it difficult for her to see
where each clue begins.  It's your job to help Bessie out and recover
the clue numbering!

An unlabeled crossword is given to you as an N by M grid (3 <= N <=
50, 3 <= M <= 50).  Some cells will be clear (typically colored white)
and some cells will be blocked (typically colored black). Given this
layout, clue numbering is a simple process which follows two logical
steps:

Step 1: We determine if a each cell begins a horizontal or vertical
clue.  If a cell begins a horizontal clue, it must be clear, its
neighboring cell to the left must be blocked or outside the crossword
grid, and the two cells on its right must be clear (that is, a
horizontal clue can only represent a word of 3 or more characters).
The rules for a cell beginning a vertical clue are analogous: the cell
above must be blocked or outside the grid, and the two cells below
must be clear.

Step 2: We assign a number to each cell that begins a clue.  Cells are
assigned numbers sequentially starting with 1 in the same order that
you would read a book; cells in the top row are assigned numbers from
left to right, then the second row, etc.  Only cells beginning a clue
are assigned numbers.

For example, consider the grid, where '.' indicates a clear cell and
'#' a blocked cell.

...
#..
...
..#
.##

Cells that can begin a horizontal or vertical clue are marked with !
below:

!!!
#..
!..
..#
.##

If we assign numbers to these cells, we get the following;

123
#..
4..
..#
.##

Note that crossword described in the input data may not satisfy
constraints typically seen in published crosswords.  For example, some
clear cells may not be part of any clue.

输入描述

The first line of input contains N and M separated by a space.
The next N lines of input each describe a row of the grid. Each
contains M characters, which are either '.' (a clear cell) or '#' (a
blocked cell).

输出描述

On the first line of output, print the number of clues.
On the each remaining line, print the row and column giving the
position of a single clue (ordered as described above). The top left
cell has position (1, 1). The bottom right cell has position (N, M).

示例1

输入:

5 3
...
#..
...
..#
.##

输出:

4
1 1
1 2
1 3
3 1

原站题解

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

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 488K, 提交时间: 2020-04-25 23:33:23

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 50+10;
char a[MAXN][MAXN]; vector<pair<int,int> > ans;
int main(){
    int n,m; scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%s",a[i]);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            bool ok=false;
            if(i+2<n && a[i][j]=='.' && a[i+1][j]=='.' && a[i+2][j]=='.' && (i==0 || a[i-1][j]=='#'))
                ok=true;
            if(j+2<m && a[i][j]=='.' && a[i][j+1]=='.' && a[i][j+2]=='.' && (j==0 || a[i][j-1]=='#'))
                ok=true;
            if(ok) ans.push_back({i+1,j+1});
        }
    }
    printf("%d\n",ans.size());
    for(auto v:ans) printf("%d %d\n",v.first,v.second);
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 488K, 提交时间: 2020-04-27 15:29:21

#include<bits/stdc++.h>
using namespace std;
char Map[100][100];
int m,n;
int x[100005],y[10005];
int ans;
int main()
{
	cin>>m>>n;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
			cin>>Map[i][j];
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
		{
			if(Map[i][j]!='.')
				continue;
			if((i==1||Map[i-1][j]=='#')&&Map[i+1][j]=='.'&&Map[i+2][j]=='.')
			{
				x[++ans]=i;
				y[ans]=j;
			}
			else if((j==1||Map[i][j-1]=='#')&&Map[i][j+1]=='.'&&Map[i][j+2]=='.')
			{
				x[++ans]=i;
				y[ans]=j;
			}
		}
		cout<<ans<<endl;
	for(int i=1;i<=ans;i++)
		cout<<x[i]<<" "<<y[i]<<endl;
	return 0;
}

上一题