#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> ret;
vector<int> res;
void dfs (vector<bool> &used) {
if (res.size() == 8) {ret.push_back(res);return;}
for (int i = 1; i <= 8; i++) {
if (used[i]) continue;
used[i] = true;
res.push_back(i);
dfs(used);
res.pop_back();
used[i] = false;
}
}
int main() {
vector<bool> used(8, false);
dfs(used);
for (int i = 0; i < ret.size(); i++) {
for (int j = 0; j < ret[i].size(); j++) cout << ret[i][j] << " ";
cout << "\n";
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int path[100];
bool s[100];
void dfs(int u) {
if(u > n) {
for (int i = 1; i <= n; i++){
cout << path[i] << " ";
}
cout << endl;
}
for (int i = 1; i <= n; i++) {
if(!s[i]) {
s[i] = true;
path[u] = i;
dfs(u + 1);
s[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
dfs
class Solution {
public:
bool used[1100];
void dfs(vector<vector<int>>&rooms, int x) {
for (auto &y : rooms[x]) {
if (!used[y]){
used[y] = true;
dfs(rooms, y);}
}
}
bool canVisitAllRooms(vector<vector<int>>& rooms) {
dfs(rooms, 0);
used[0] = true;
for (int i = 0; i < rooms.size(); i++) {
if (!used[i]) return false;
}
return true;
}
};
bfs
class Solution {
public:
bool used[1100];
void bfs(vector<vector<int>> &rooms, int x) {
queue<int> que;
que.push(0);
while (!que.empty()) {
auto it = que.front();que.pop();
for (auto y : rooms[it]) {
if (used[y] == false) {
used[y] = true;
que.push(y);
}
}
}
}
bool canVisitAllRooms(vector<vector<int>>& rooms) {
used[0] = true;
bfs(rooms, 0);
for (int i = 0; i < rooms.size(); i++) {
if(!used[i]) return false;
}
return true;
}
};
#include<bits/stdc++.h>
using namespace std;
const int N = 510;
char a[N][N];
int st[N][N];
int n, m, flag = 0;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
void dfs(int xx, int yy) {
if (a[xx][yy] == 'E') flag = 1;
for (int i = 0; i < 4; i++) {
int x = dx[i] + xx, y = dy[i] + yy;
if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '#' && !st[x][y]) {
st[x][y] = 1;
dfs(x, y);
}
}
}
int main() {
while (cin >> n >> m) {
flag = 0;
int x1, y1;
memset(a, 0, sizeof a);
memset(st, 0, sizeof st);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') {x1 = i; y1 = j;}
}
}
dfs(x1, y1);
if (flag) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
const int N = 20;
char a[N][N];
bool judge(int start, int end) {
for (int i = 0; i <= start; i++) if(a[i][end] == 'Q') return false;
for (int i = 0; i <= end; i++) if(a[start][i] == 'Q') return false;
for (int i = start, j = end; i >= 0 && j >= 0; i--, j--) if(a[i][j] == 'Q') return false;
for (int i = start, j = end; i >= 0 && j < n; i--, j++) if(a[i][j] == 'Q') return false;
return true;
}
void dfs(int u) {
if(u == n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << a[i][j];
}
cout << endl;
}
cout << endl;
}
for (int j = 0; j < n; j++) {
if (judge(u, j)) {
a[u][j] = 'Q';
dfs(u + 1);
a[u][j] = '.';
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = '.';
}
}
dfs(0);
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
queue<PII>q;
int n, m;
const int N = 110;
int a[N][N],s[N][N];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int bfs() {
q.push({0, 0});
while(q.size()) {
auto it = q.front();q.pop();
for (int i = 0; i < 4; i++) {
int x = it.x + dx[i], y = it.y + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && a[x][y] == 0 && s[x][y] == 0) {
s[x][y] = s[it.x][it.y] + 1;
q.push({x,y});
}
}
}
return s[n - 1][m - 1];
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
cout << bfs();
return 0;
}