wty

113

fanruiyang2013

wty
12天前
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 25;

int n, m;
char g[N][N];
bool st[N][N];

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int dfs(int x, int y)
{
int cnt = 1;

st[x][y] = true;
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (g[a][b] != '.') continue;
if (st[a][b]) continue;

cnt += dfs(a, b);
}

return cnt;
}

int main()
{
while (cin >> m >> n, n || m)
{
for (int i = 0; i < n; i ++ ) cin >> g[i];

int x, y;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (g[i][j] == '@')
{
x = i;
y = j;
}

memset(st, 0, sizeof st);
cout << dfs(x, y) << endl;
}

return 0;
}


wty
13天前
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 110;

int n;
char g[N][N];
int xa, ya, xb, yb;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool st[N][N];

bool dfs(int x, int y)
{
if (g[x][y] == '#') return false;
if (x == xb && y == yb) return true;

st[x][y] = true;

for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n) continue;
if (st[a][b]) continue;
if (dfs(a, b)) return true;
}

return false;
}

int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
scanf("%d%d%d%d", &xa, &ya, &xb, &yb);

memset(st, 0, sizeof st);
if (dfs(xa, ya)) puts("YES");
else puts("NO");
}

return 0;
}


wty
5个月前
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
vector<int> vec(arr);
sort(arr.begin(), arr.end());
int ans = 0;
for(int i = 0; i < vec.size(); i++){
for(int j = 0; j < arr.size(); j++){
if(arr[j] != -1 && arr[j] == vec[i]){
vec[i] = j;
arr[j] = -1;
break;
}
}
}
for(int i = 0; i < vec.size(); ){
if(vec[i] == i){
ans++;
i++;
}
else if(vec[i] == vec.size() - 1){
ans++;
break;
}
else if(vec[i] > i){
int index = vec[i];
int j = i + 1;
while(j <= index){
if(vec[j] > index){
index = vec[j];
}
j++;
}
if(j == vec.size()){
ans++;
break;
}
else{
ans++;
i = index + 1;
}
}
}

return ans;
}
};


wty
5个月前
//直接暴力
class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
vector<vector<int>> ret;
vector<int> vec;
for(int i = 0; i < groupSizes.size(); i++){
if(groupSizes[i] == 0){
continue;
}
int t = groupSizes[i];
vec.push_back(i);
int k = 1;
for(int j = i + 1; j < groupSizes.size(); j++){
if(groupSizes[j] != 0 && k < t && groupSizes[j] == t){
vec.push_back(j);
groupSizes[j] = 0;
k++;
}
if(k == t){
break;
}
}
ret.push_back(vec);
vec.clear();
}
return ret;
}
};


wty
5个月前