//最少点路径
#include<bits/stdc++.h>
using namespace std;
int n,s,t,a[11][11],xwz[11];
struct node{
int wz;
string lj;
};
int vis[11];
string xlj[11];
queue<node> q;
void bfs(int x){
q.push({x,""});
vis[x]=1;
while(!q.empty()){
int twz=q.front().wz;
string tlj=q.front().lj;
q.pop();
for(int i=1; i<=n; i++){
if((a[twz][i]==1||a[i][twz]==1)&&vis[i]==0){
a[twz][i]=0;a[i][twz]=0;
vis[i]=1;
xwz[i]=i;
xlj[i]=tlj+'-'+char(i+48);
q.push({xwz[i],xlj[i]});
if(xwz[i]==t) {
cout<<s;
cout<<xlj[i]<<endl;
return ;
}
}
}
}
}
int main(){
cin>>n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cin>>a[i][j];
for(int i=0; i<=11; i++){
vis[i]=0;
}
cin>>s>>t;
bfs(s);
return 0;
}
//八数码问题
#include<bits/stdc++.h>
using namespace std;
int mat[3][3];
int r,c;
int nc,nr;
map<int,int>vis;
map<int,int>step;
int start,result,k=0;
queue<int> Q;
int dir[4][2]={-1,0,0,1,1,0,0,-1};
int re[100];
void input(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>mat[i][j];
}
}
}
int changetosum(){
int start=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
start=start*10+mat[i][j];
}
}
return start;
}
void changetoarc(int start){
for(int i=2;i>=0;i--){
for(int j=2;j>=0;j--){
mat[i][j]=start%10;
start/=10;
if(mat[i][j]==0){
r=i;c=j;
}
}
}
}
int can_move(int start,int i){
changetoarc(start);
if(i==0&&r==0||i==1&&c==2||i==2&&r==2||i==3&&c==0) return 0;
else return 1;
}
int move_to(int start,int i){
nr=r+dir[i][0];
nc=c+dir[i][1];
mat[r][c]=mat[nr][nc];
mat[nr][nc]=0;
return changetosum();
}
int main(){
cin>>start>>result;
vis[start]=1;
step[start]=0;
Q.push(start);
while(Q.size()){
int u,v;
u=Q.front();
Q.pop();
if(u==result){
cout<<step[u]<<endl;
return 0;
}
for(int i=0;i<4;i++){
if(can_move(u,i)){
v=move_to(u,i);
if(!vis[v]){
Q.push(v);
vis[v]=1;
step[v]=step[u]+1;
re[step[v]]=i;
}
}
}
}
cout<<"impossible"<<endl;
return 0;
}
//联通块
#include<bits/stdc++.h>
using namespace std;
int dx[4]={1,-1,0,0},
dy[4]={0,0,1,-1};
int bz[101][101],num=0;
char s[101][101],ch;
bool vis[101][101];
int m,n;
void bfs(int p,int q){
int x,y,t,w,i;
int h[1001][3];
num++;bz[p][q]=0;
t=0;w=1;
h[1][1]=p;
h[1][2]=q;
vis[p][q]=true;
while(t<w){
t++;
for(int i=0; i<=3; i++){
x=h[t][1]+dx[i];
y=h[t][2]+dy[i];
if((x>=0)&&(x<m)&&(y>=0)&&(y<n)&&(bz[x][y]!=0)&&!vis[x][y]){
w++;
h[w][1]=x;
h[w][2]=y;
bz[x][y]=0;
vis[x][y]=true;
}
}
}
}
int main()
{
cin>>m>>n;
for(int i=0; i<=m-1; i++)
for(int j=0; j<=n-1; j++)
bz[i][j]=1;
for(int i=0; i<=m-1; i++){
for(int j=0; j<=n-1; j++){
cin>>s[i][j];
if(s[i][j]=='0') bz[i][j]=0;
}
}
for(int i=0; i<=m-1; i++)
for(int j=0; j<=n-1; j++)
if(bz[i][j]!=0&&!vis[i][j]) bfs(i,j);
cout<<num<<endl;
return 0;
}
//魔法鲜花
# include <bits/stdc++.h>
using namespace std;
const int maxN=1e5+1;
string s[4];
struct node{
int wz,step;
string cs;
};
queue<node>Q;
int n,k;//n是John 初始位置,k是奶牛的位置
int vis[2*maxN];
void bfs(int start){
Q.push({start,0});
vis[start]=1;//标记这个数字出现过了
while (!Q.empty()){
//取头节点数据
int touwz=Q.front().wz;
int toustep=Q.front().step;
string tou=Q.front().cs;
Q.pop();//弹出头节点(删除头节点)
//根据头节点数据产生新的节点数据
int newwz[4];
newwz[1]=touwz+1; s[1]=tou+'A';
newwz[2]=touwz-10; s[2]=tou+'B';
newwz[3]=touwz*2; s[3]=tou+'C';
int newstep=toustep+1;
for (int i=1;i<=3;i++){
if (newwz[i]==k) {
cout<<newstep<<endl;
cout<<s[i]<<endl;
return;
}
else if(vis[newwz[i]]==0 && newwz[i]>0 && newwz[i]<=maxN-1) {
Q.push({newwz[i],newstep,s[i]});
vis[newwz[i]]=1;//标记出现过了
}
}
}
}
int main(){
cin>>n>>k;
if (n==k) cout<<0<<endl;
else
bfs(n);
return 0;
}
//抓住那头牛
#include <bits/stdc++.h>
#define N 100001
using namespace std;
bool vis[N];
int dir[2]={-1,1};
struct node
{
int x;
int step;
}q[N];
void bfs(int n,int k)
{
int head=1,tail=1;
vis[n]=1;
q[tail].x=n;
q[tail].step=0;
tail++;
while(head<tail)
{
int x=q[head].x;
int step=q[head].step;
if(x==k)
{
cout<<step<<endl;
break;
}
for(int i=0;i<2;i++)
{
int nx=x+dir[i];
if(1<=nx and nx<=N and vis[nx]==0)
{
vis[nx]=1;
q[tail].x=nx;
q[tail].step=step+1;
tail++;
}
}
int nx=2*x;
if(1<=nx and nx<=N and vis[nx]==0)
{
vis[nx]=1;
q[tail].x=nx;
q[tail].step=step+1;
tail++;
}
head++;
}
}
int main()
{
int n,k;
cin>>n>>k;
if(k<n)
{
cout<<n-k<<endl;
}
else
{
bfs(n,k);
}
return 0;
}
//魔板
#include <bits/stdc++.h>
using namespace std;
struct node{
string mbzt;
string dongzuochuan;
};
queue <node> q;
set<string>s;
string start_zt="12345678";
string target_zt;
string A_shangxia(string mbzt){
string temp;
temp=mbzt.substr(4,4)+mbzt.substr(0,4);
return temp;
}
string B_youyi(string mbzt){
string temp;
temp=mbzt.substr(3,1)+mbzt.substr(0,3)+mbzt.substr(7,1)+mbzt.substr(4,3);
return temp;
}
string C_zhongjianzhuan(string mbzt){
string temp;
temp=mbzt.substr(0,1)+mbzt.substr(5,1)+mbzt.substr(1,1)+mbzt.substr(3,1)+mbzt.substr(4,1)+mbzt.substr(6,1)+mbzt.substr(2,1)+mbzt.substr(7,1);
return temp;
}
int main()
{
//cin>>target_zt;
target_zt="";
for(int i=0;i<8;i++){
int d;
cin>>d;
target_zt=target_zt+char(d+48);
}
//初始状态入队
q.push({target_zt,""});
s.insert(target_zt);
while (!q.empty()){
//取头状态数据
string tzhuangtai=q.front().mbzt; string tdongzuochuan=q.front().dongzuochuan;
//删除头节点
q.pop();
//依据规则产生状态
string tempzt[3];
tempzt[0]=A_shangxia(tzhuangtai); tempzt[1]=B_youyi(tzhuangtai); tempzt[2]=C_zhongjianzhuan(tzhuangtai);
//为动作串增加新动作
string dongzuochuan[3];
dongzuochuan[0] =tdongzuochuan+"A"; dongzuochuan[1] =tdongzuochuan+"B"; dongzuochuan[2] =tdongzuochuan+"C";
//检查刚产生的3个状态情况
for(int i=0;i<3;i++){
//如果是目标状态则输出
if (tempzt[i]==start_zt){
cout<<dongzuochuan[i]<<endl;
return 0;
}
//如果不是目标,且是新状态???
if(s.count(tempzt[i])==0){
q.push({tempzt[i],dongzuochuan[i]});
s.insert(tempzt[i]);
}
}
}
cout<<"wujie"<<endl;
return 0;
}