#include<iostream>
#include<queue>
using namespace std;
const int N=20+5;
char mp[N][N];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
int num;
int n,m;
struct node{
int x;
int y;
};
bool check(int x,int y){
if(x>=1 && x<=n && y>=1 && y<=m){
return 1;
}
else{
return 0;
}
}
void bfs(int dx,int dy){
queue<node>q;
node start,next;
start.x=dx;
start.y=dy;
q.push(start);
num++;
while(!q.empty()){
start=q.front();
q.pop();
for(int i=0;i<4;i++){
next.x=start.x+dir[i][0];
next.y=start.y+dir[i][1];
if(check(next.x,next.y) && mp[next.x][next.y]=='.'){
mp[next.x][next.y]='#';
num++;
q.push(next);
}
}
}
}
int main()
{
int stx,sty;
while(cin >> m >> n && n && m){
num=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
if(mp[i][j]=='@'){
stx=i;
sty=j;
}
}
}
bfs(stx,sty);
cout << num << endl;
}
return 0;
}
#include<iostream>
using namespace std;
const int N=20+5;
char mp[N][N];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
int num;
int n,m;
bool check(int x,int y){
if(x>=1 && x<=n && y>=1 && y<=m){
return 1;
}
else{
return 0;
}
}
void dfs(int dx,int dy){
for(int i=0;i<4;i++){
int nowx=dx+dir[i][0];
int nowy=dy+dir[i][1];
if(check(nowx,nowy) && mp[nowx][nowy]=='.'){
mp[nowx][nowy]='#';
num++;
dfs(nowx,nowy);
}
}
}
int main()
{
int stx,sty;
while(cin >> m >> n && n && m){
num=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
if(mp[i][j]=='@'){
num++;
stx=i;
sty=j;
}
}
}
dfs(stx,sty);
cout << num << endl;
}
return 0;
}
#include<iostream>
#include<queue>
#include<map>
#include<cstdlib>
using namespace std;
const int MAXN=1e6+5;
char op[4]={'u','d','l','r'};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int fac[9]={ 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int vis[MAXN];
int flag;
int idx;
char path[MAXN];
int pre[MAXN];
struct node{
int s[9];
int c;
int loc;
};
int cantor(int s[]){
int ans=1;
for(int i=0;i<9;i++){
int tmp=0;
for(int j=i+1;j<9;j++){
if(s[i]>s[j]) tmp++;
}
ans=ans+tmp*fac[9-i-1];
}
return ans;
}
bool check(int x,int y){
if(x>=0 && x<=2 && y>=0 && y<=2){
return 1;
}
return 0;
}
void bfs(node start){
queue<node>q;
node next;
vis[start.c]=1;
pre[start.c]=-1;
q.push(start);
while(!q.empty()){
start=q.front();
q.pop();
if(start.c==1){
flag=1;
return;
}
int x=start.loc/3;
int y=start.loc%3;
for(int i=0;i<4;i++){
int nowx=x+dir[i][0];
int nowy=y+dir[i][1];
if(check(nowx,nowy)){
next=start;
swap(next.s[nowx*3+nowy],next.s[x*3+y]);
next.c=cantor(next.s);
if(!vis[next.c]){
vis[next.c]=1;
next.loc=nowx*3+nowy;
pre[next.c]=start.c;
path[next.c]=op[i];
q.push(next);
}
}
}
}
return;
}
void pr(int x){
if(pre[x]==-1){
return;
}
pr(pre[x]);
cout << path[x];
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
string str;
int cnt=0;
node st;
for(int i=0;i<9;i++){
cin >> str;
if(str=="x"){
idx=i;
st.s[cnt++]=9;
}
else st.s[cnt++]=atoi(str.c_str());
}
st.loc=idx;
st.c=cantor(st.s);
flag=0;
bfs(st);
if(!flag){
cout << "unsolvable" << endl;
}
else{
pr(1);
cout << endl;
}
return 0;
}
hdu1312题目传送门:
https:
poj1077题解传送门:
https:
https: