輸出個數:
#include<iostream>
#include<string>
using namespace std;
const size_t row = 21, column = 21;
char str[row][column];
int w, h;
int f(int x, int y)
{
if(x < 0 || y < 0 || x >= h || y >= w) //超區矩陣範圍
return 0;
if(str[x][y] == '#')
return 0;
else
{
str[x][y] = '#'; //標記走過的磚
return f(x, y-1) + f(x, y+1) + f(x-1,y) + f(x+1,y) + 1;
}
}
int main()
{
cout << "Enter the row and the column: " << endl;
cin >> w >> h;
cout << "Enter the stings: " << endl;
for(size_t ix = 0; ix != h; ++ix)
for(size_t index = 0; index != w; ++index)
cin >> str[ix][index];
for(size_t ix = 0; ix != h; ++ix)
for(size_t index = 0; index != w; ++index)
if(str[ix][index] == '@')
cout << f(ix,index) << endl;
return 0;
}
結果:45
輸出走過的路徑(不重複):
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
const size_t row = 21, column = 21;
char str[row][column];
int a[row][column];
int w, h;
int f(int x, int y)
{
int m, n, flag = 1;
char str1[200] = {}, str2[200] = {}, str3[500] = {};
if(x < 0 || y < 0 || x >= h || y >= w)
return 0;
if(str[x][y] == '#')
return 0;
else
{
itoa(x, str1, 10);
itoa(y, str2, 10);
strcat(str3, "(");
strcat(str3, str1);
strcat(str3, "!");
strcat(str3, str2);
strcat(str3, ")\t");
if(a[x][y] == 0)
cout << str3;
a[x][y] =1;
if(f(x-1,y) != 0 && f(x, y+1) != 0 && flag)
{
m = x;
n = y;
flag = 0;
}
str[x][y] = '#';
if(f(x-1, y) != 0)
f(x-1,y);
else if(f(x, y+1) != 0)
f(x, y+1);
else if(f(x+1, y) != 0)
f(x+1, y);
else if(f(x, y-1) != 0)
f(x, y-1);
else
f(m, n-1);
}
}
int main()
{
cout << "Enter the wideth and the hight: " << endl;
cin >> w >> h;
for(size_t ix = 0; ix != h; ++ix)
for(size_t index = 0; index != w; ++index)
{
cin >> str[ix][index];
if(str[ix][index] == '#')
a[ix][index] = 1;
}
for(size_t ix = 0; ix != h; ++ix)
for(size_t index = 0; index != w; ++index)
if(str[ix][index] == '@')
{
f(ix, index);
break;
}
cout << endl;
return 0;
}
//輸出走過的路徑(重複):
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
const size_t row = 21, column = 21;
char str[row][column];
//int a[row][column];
int w, h;
int f(int x, int y)
{
int m, n, flag = 1;
char str1[200] = {}, str2[200] = {}, str3[500] = {};
if(x < 0 || y < 0 || x >= h || y >= w)
return 0;
if(str[x][y] == '#')
return 0;
else
{
itoa(x, str1, 10);
itoa(y, str2, 10);
strcat(str3, "(");
strcat(str3, str1);
strcat(str3, "!");
strcat(str3, str2);
strcat(str3, ")\t");
//if(a[x][y] == 0)
cout << str3;
//a[x][y] =1;
if(f(x-1,y) != 0 && f(x, y+1) != 0 && flag)
{
m = x;
n = y;
flag = 0;
}
str[x][y] = '#';
if(f(x-1, y) != 0)
f(x-1,y);
else if(f(x, y+1) != 0)
f(x, y+1);
else if(f(x+1, y) != 0)
f(x+1, y);
else if(f(x, y-1) != 0)
f(x, y-1);
else
f(m, n-1);
}
}
int main()
{
cout << "Enter the wideth and the hight: " << endl;
cin >> w >> h;
for(size_t ix = 0; ix != h; ++ix)
for(size_t index = 0; index != w; ++index)
{
cin >> str[ix][index];
//if(str[ix][index] == '#')
// a[ix][index] = 1;
}
for(size_t ix = 0; ix != h; ++ix)
for(size_t index = 0; index != w; ++index)
if(str[ix][index] == '@')
{
f(ix, index);
break;
}
cout << endl;
return 0;
}
同時歡迎提出寶貴意見,以幫助我改進,不勝感激!!!
——桑海整理