Altered Structure
This commit is contained in:
66
Algorithm/DS-Related/Graph/BFS/Maze.cpp
Normal file
66
Algorithm/DS-Related/Graph/BFS/Maze.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <deque>
|
||||
using namespace std;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int dx[4] = {0, 0, -1, 1};
|
||||
int dy[4] = {1, -1, 0, 0};
|
||||
int dxx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
|
||||
int dyy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
|
||||
int main(){
|
||||
deque<pair<int, int>> q;
|
||||
int n, m;
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
cin >> n >> m;
|
||||
int startx, starty;
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
cin >> startx >> starty;
|
||||
|
||||
deque<deque<int>> maze;
|
||||
deque<deque<bool>> visited;
|
||||
deque<deque<int>> dist(n, deque<int>(m, 0)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
maze.resize(n);
|
||||
visited.resize(n, deque<bool>(m, false));
|
||||
int tmp;
|
||||
//0<><30>ʾǽ<CABE><C7BD>1<EFBFBD><31>ʾ·
|
||||
for(int i = 0; i < n; i++){
|
||||
for(int j = 0; j < m; j++){
|
||||
cin >> tmp;
|
||||
maze[i].push_back(tmp);
|
||||
}
|
||||
}
|
||||
tmp = 0;
|
||||
q.push_back(make_pair(startx, starty));
|
||||
while(!q.empty()){
|
||||
pair<int, int> u = q.front();
|
||||
q.pop_front();
|
||||
int x = u.first, y = u.second;
|
||||
visited[x][y] = true;
|
||||
|
||||
for(int i = 0; i < 4; i++){
|
||||
if(x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m && !visited[x + dx[i]][y + dy[i]] && maze[x + dx[i]][y + dy[i]] == 1){
|
||||
visited[x + dx[i]][y + dy[i]] = true;
|
||||
dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1; // <20><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD>
|
||||
q.push_back(make_pair(x + dx[i], y + dy[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
for(int i = 0; i < n; i++){
|
||||
for(int j = 0; j < m; j++){
|
||||
cout << dist[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
6 5
|
||||
0 0
|
||||
1 1 0 1 1
|
||||
1 0 1 1 1
|
||||
1 0 1 0 0
|
||||
1 0 1 1 1
|
||||
1 1 1 0 1
|
||||
1 1 1 1 1
|
||||
*/
|
@@ -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/DS-Related/Graph/DFS/UVa572-Oil-Deposits.cpp
Normal file
51
Algorithm/DS-Related/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;
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
struct Graph{
|
||||
int v;
|
||||
int e;
|
||||
vector<vector<int>> adj;
|
||||
vector<bool> visited;
|
||||
|
||||
Graph(int _v) : v(_v) {
|
||||
adj.resize(v);
|
||||
visited.assign(v, false);
|
||||
}
|
||||
bool validateV(int _v){
|
||||
return ( _v >= 0 && _v < v);
|
||||
}
|
||||
void insert(int _a, int _b){
|
||||
if(!validateV(_a) || !validateV(_b)) return;
|
||||
|
||||
adj[_a].push_back(_b);
|
||||
}
|
||||
int degree(int _v){
|
||||
if(!validateV(_v)) return 0;
|
||||
return outDegree(_v) + inDegree(_v);
|
||||
}
|
||||
int outDegree(int _v){
|
||||
if(!validateV(_v)) return 0;
|
||||
return adj[_v].size();
|
||||
}
|
||||
int inDegree(int _v){
|
||||
if(!validateV(_v)) return 0;
|
||||
int js = 0;
|
||||
for(auto x : adj){
|
||||
for(int i = 0; i < x.size(); i++){
|
||||
if(x[i] == _v) js++;
|
||||
}
|
||||
}
|
||||
return js;
|
||||
}
|
||||
void dfs(int _v){
|
||||
if(!validateV(_v)) return;
|
||||
if(visited[_v]) return;
|
||||
visited[_v] = true;
|
||||
for(auto x : adj[_v]){
|
||||
dfs(x);
|
||||
}
|
||||
}
|
||||
bool isConnected(){
|
||||
dfs(0);
|
||||
for(auto x : visited){
|
||||
if(!x) return false;
|
||||
}
|
||||
|
||||
int js = 0;
|
||||
for(int i = 0; i < v; i++){
|
||||
if(degree(i) % 2 == 1) js++;
|
||||
}
|
||||
return (js == 0) || (js == 2);
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
int n;
|
||||
cin >> n;
|
||||
Graph g(n);
|
||||
vector<string> s;
|
||||
s.resize(n);
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> s[i];
|
||||
}
|
||||
for(int i = 0; i < n; i++){
|
||||
for(int j = 0; j < n; j++){
|
||||
if(i != j && s[i].back() == s[j].front()) g.insert(i, j);
|
||||
}
|
||||
}
|
||||
g.isConnected() ? cout << "YES" << endl : cout << "NO" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
string line;
|
||||
while (getline(cin, line)) { // һ<>ζ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>У<EFBFBD>ֱ<EFBFBD><D6B1> EOF
|
||||
list<char> s;
|
||||
auto it = s.begin();
|
||||
|
||||
for (char tmp : line) {
|
||||
if (tmp == '[') {
|
||||
it = s.begin();
|
||||
}
|
||||
else if (tmp == ']') {
|
||||
it = s.end();
|
||||
}
|
||||
else {
|
||||
it = s.insert(it, tmp);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
for (char c : s) cout << c;
|
||||
cout << '\n'; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user