Altered
This commit is contained in:
63
Algorithm/Graph/DFS/UVa10603-Fill.cpp
Normal file
63
Algorithm/Graph/DFS/UVa10603-Fill.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
bool have = false;
|
||||
set<tuple<int,int,int>> visited;
|
||||
|
||||
int A, B, C; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
void dfs(int a, int b, int c, int target) {
|
||||
if (a == target || b == target || c == target) {
|
||||
have = true;
|
||||
return;
|
||||
}
|
||||
|
||||
auto state = make_tuple(a, b, c);
|
||||
if (visited.find(state) != visited.end()) return;
|
||||
visited.insert(state);
|
||||
|
||||
// <20><><EFBFBD>ֵ<EFBFBD>ˮ<EFBFBD><CBAE><EFBFBD><EFBFBD>
|
||||
// a -> b
|
||||
{
|
||||
int pour = min(a, B - b);
|
||||
dfs(a - pour, b + pour, c, target);
|
||||
}
|
||||
// a -> c
|
||||
{
|
||||
int pour = min(a, C - c);
|
||||
dfs(a - pour, b, c + pour, target);
|
||||
}
|
||||
// b -> a
|
||||
{
|
||||
int pour = min(b, A - a);
|
||||
dfs(a + pour, b - pour, c, target);
|
||||
}
|
||||
// b -> c
|
||||
{
|
||||
int pour = min(b, C - c);
|
||||
dfs(a, b - pour, c + pour, target);
|
||||
}
|
||||
// c -> a
|
||||
{
|
||||
int pour = min(c, A - a);
|
||||
dfs(a + pour, b, c - pour, target);
|
||||
}
|
||||
// c -> b
|
||||
{
|
||||
int pour = min(c, B - b);
|
||||
dfs(a, b + pour, c - pour, target);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int target;
|
||||
cin >> A >> B >> C >> target;
|
||||
dfs(A, 0, 0, target); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC> A װ<><D7B0>
|
||||
if (have) cout << "YES" << endl;
|
||||
else cout << "NO" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
32
Algorithm/Graph/DFS/UVa1600Knight-Move betteruseBSF.cpp
Normal file
32
Algorithm/Graph/DFS/UVa1600Knight-Move betteruseBSF.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//ab<61><62><EFBFBD>յ<EFBFBD>
|
||||
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
|
||||
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
|
||||
int step[9][9];
|
||||
|
||||
void dfs(int _a, int _b, int _n, int _m){
|
||||
if(_n > 8 || _n < 1 || _m < 1 || _m > 8) return;
|
||||
if(_n == _a && _m == _b) return;
|
||||
for(int i = 0; i < 8; i++){
|
||||
int nx = _n + dx[i];
|
||||
int ny = _m + dy[i];
|
||||
|
||||
if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
|
||||
int stepnew = step[_n][_m] + 1;
|
||||
if (step[nx][ny] == 0 || stepnew < step[nx][ny]) {
|
||||
step[nx][ny] = stepnew;
|
||||
dfs(_a, _b, nx, ny);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int a, b, n, m;
|
||||
cin >> n >> m >> a >> b;
|
||||
step[n][m] = 0;
|
||||
dfs(a, b, n, m);
|
||||
cout << step[a][b] << endl;
|
||||
return 0;
|
||||
}
|
||||
51
Algorithm/Graph/DFS/UVa572-Oil-Deposits.cpp
Normal file
51
Algorithm/Graph/DFS/UVa572-Oil-Deposits.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int m = 5, n = 5;
|
||||
vector<vector<char>> s;
|
||||
vector<vector<bool>> visited;
|
||||
vector<vector<int>> ids;
|
||||
|
||||
|
||||
void dfs(int x, int y, int id){
|
||||
if(x < 0 || x >= m || y < 0 || y >=n) return;
|
||||
if(s[x][y] != '@') return;
|
||||
if(visited[x][y] == true) return;
|
||||
visited[x][y] = true;
|
||||
ids[x][y] = id;
|
||||
dfs(x - 1, y - 1, id + 1);
|
||||
dfs(x - 1, y, id + 1);
|
||||
dfs(x - 1, y + 1, id + 1);
|
||||
dfs(x, y - 1, id + 1);
|
||||
dfs(x, y + 1, id + 1);
|
||||
dfs(x + 1, y - 1, id + 1);
|
||||
dfs(x + 1, y, id + 1);
|
||||
dfs(x + 1, y + 1, id + 1);
|
||||
cout << id << " ";
|
||||
}
|
||||
|
||||
int main(){
|
||||
s.resize(m);
|
||||
visited.assign(m, vector<bool>(n, false));
|
||||
ids.assign(m, vector<int>(n, 0));
|
||||
char tmp;
|
||||
for(int i = 0; i < m; i++){
|
||||
for(int j = 0; j < n; j++){
|
||||
cin >> tmp;
|
||||
s[i].push_back(tmp);
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < m; i++){
|
||||
for(int j = 0; j < n; j++){
|
||||
if(!visited[i][j] && s[i][j] == '@') dfs(i, j, 1);
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
int max = -1;
|
||||
for(int i = 0; i < m; i++){
|
||||
for(int j = 0; j < n; j++){
|
||||
if(ids[i][j] > max) max = ids[i][j];
|
||||
}
|
||||
}
|
||||
cout << max << endl;
|
||||
}
|
||||
40
Algorithm/Graph/DFS/UVa673-Parentheses-Balance([]).cpp
Normal file
40
Algorithm/Graph/DFS/UVa673-Parentheses-Balance([]).cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
//确定每个节点要干什么
|
||||
bool dfs(const string& s, int l, int r) {
|
||||
if (l > r) return true; // 空串平衡
|
||||
if (l == r) return false; // 单个字符不平衡
|
||||
|
||||
// 必须匹配类型
|
||||
char open = s[l];
|
||||
char close = (open == '(') ? ')' : (open == '[') ? ']' : 0;
|
||||
if (close == 0) return false; // 非括号字符
|
||||
|
||||
int cnt = 0;
|
||||
int p = -1;
|
||||
for (int i = l; i <= r; i++) {
|
||||
if (s[i] == '(' || s[i] == '[') cnt++;
|
||||
if (s[i] == ')' || s[i] == ']') cnt--;
|
||||
if (cnt == 0) {
|
||||
p = i; // 找到匹配的右括号
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (p == -1) return false; // 没找到匹配
|
||||
if (s[p] != close) return false; // 类型不匹配(第一个和最后一个字符匹配)
|
||||
|
||||
// 内部和剩余区间递归
|
||||
return dfs(s, l + 1, p - 1) && dfs(s, p + 1, r);
|
||||
}
|
||||
|
||||
int main(){
|
||||
string a;
|
||||
while(true)
|
||||
{
|
||||
cin >> a;
|
||||
dfs(a, 0, a.size() - 1) ? cout << "YES" << endl : cout << "NO" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user