NC24446. [USACO 2014 Dec B]Crosswords
描述
输入描述
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; }