题意: 每次给出一张30*100的矩阵,其中"#"表示车牌字迹部分,"."表示黑色背景。现需要你找到7个字的左右边界。
思路:直接一列一列扫描判断边界即可,但是注意有一个坑——第一个汉字像"川"在这种方法下分别会有三个左右边界,于是在输出的时候对应找到汉字真正的左右边界即可。
代码实现:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
inline void read(int &x){
char t=getchar();
while(!isdigit(t)) t=getchar();
for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
int T = 1, cnt;
vector<int> l, r;
string s[N], tmp = "..............................";
signed main()
{
IOS;
cin >> T;
while(T --){
for(int i = 0; i < 30; i ++) cin >> s[i];
for(int j = 0; j < 100; j ++){
string ss, sn; //获得左侧列和当前列
if(j) for(int i = 0; i < 30; i ++)
ss += s[i][j-1];
for(int i = 0; i < 30; i ++)
sn += s[i][j];
// cout << "j: " << j << " " << ss << " " << sn << endl;
int ok = 0;
for(int i = 0; i < 30; i ++){
if(s[i][j]=='#'&&(!j||ss==tmp)){ //如果当前列不为空且左侧列为空,则为左边界
l.push_back(j);
ok = 1;
}
if(s[i][j]=='.'&&j&&ss!=tmp&&sn==tmp){ //若当前列为空且左侧列不为空,则为右边界
r.push_back(j-1);
ok = 1;
}
if(ok) break; //找到边界,直接进入下一列
}
}
cout << "Case #" << ++cnt << ":" << endl;
int xx = l.size()-1;
cout << l[0]+1 << " " << r[xx-6]+1 << endl; //第一个汉字的左右边界
for(int i = xx-5; i <= xx; i ++)
cout << l[i]+1 << " " << r[i]+1 << endl; //后续6个字母或数组的左右边界
l.clear(); r.clear();
}
return 0;
}