// C++Exercise.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
#include<string>
#include<cstring>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<sstream>
using namespace std;
int t, m, n;
typedef struct{
int steps_min;
int steps_max;
char dir;
}choose;
vector<choose> operate;
bool feasible(vector<vector<int> >& area, int x, int y, int index){
if (index == operate.size()) return true;
int start = operate[index].steps_min;
int end = operate[index].steps_max;
int k;
if (operate[index].dir == 'R'){
for (int i = start; i <= end; i++){
if (y + i < n&&area[x][y + i] != 1){
for (k = y + i; k > y; k--){
if (area[x][k]) break;
}
if (k==y&&feasible(area, x, y + i, index + 1)) return true;
}
else break;
}
}
else if (operate[index].dir == 'L'){
for (int i = start; i <= end; i++){
if (y - i >= 0&&area[x][y - i] != 1 ){
for (k = y - i; k < y; k++){
if (area[x][k]) break;
}
if (k==y&&feasible(area, x, y - i, index + 1)) return true;
}
else break;
}
}
else if (operate[index].dir == 'U'){
for (int i = start; i <= end; i++){
if (x - i >= 0&&area[x - i][y] != 1 ){
for (k = x - i; k < x; k++){
if (area[k][y]) break;
}
if (k==x&&feasible(area, x - i, y, index + 1)) return true;
}
else break;
}
}
else if (operate[index].dir == 'D'){//'D'
for (int i = start; i <= end; i++){
if (x + i < m&&area[x + i][y] != 1){
for (k = x + i; k > x; k--){
if (area[k][y]) break;
}
if (k==x&&feasible(area, x + i, y, index + 1)) return true;
}
else break;
}
}
return false;
}
int main(){
cin >> t;
while (t--){
cin >> m >> n;
vector<vector<int> > area(m, vector<int>(n, 0));
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
cin >> area[i][j];
}
}
operate.clear();
int i, j;
while (cin >> i >> j){
if (i == 0 && j == 0) break;
if (i > j) swap(i, j);
char dir;
cin >> dir;
choose temp;
temp.steps_min = i;
temp.steps_max = j;
temp.dir = dir;
operate.push_back(temp);
}
int count = 0;
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
if (area[i][j] == 1) continue;
if (feasible(area, i, j, 0)){
count++;
}
}
}
cout << count << endl;
}
return 0;
}
zoj 1019
最新推荐文章于 2018-08-27 18:39:48 发布