求助把以下皇后守卫的C++程序改写成Pascal程序!!快!! 5
#include<iostream>usingnamespacestd;#defineMaxN10constintDir[8][2]={{1,0},{0,1},{-1,0...
#include <iostream>
using namespace std;
#define MaxN 10
const int Dir[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int N, M, Ans;
char Map[MaxN][MaxN];
int CX[MaxN], CY[MaxN], CLS[MaxN + MaxN], CRS[MaxN + MaxN];
void Search(int X, int Y, int Sum)
{
if (Sum >= Ans) return;
if (X == N)
{
Ans = Sum;
return;
}
if (Y == M)
{
Search(X + 1, 0, Sum);
return;
}
if (Map[X][Y] == 'X' && !CX[X] && !CY[Y] && !CLS[X + Y] && !CRS[X - Y + M])
{
++ CX[X], ++ CY[Y], ++ CLS[X + Y], ++ CRS[X - Y + M];
Search(X + 1, 0, Sum + 1);
-- CX[X], -- CY[Y], -- CLS[X + Y], -- CRS[X - Y + M];
for (int i = 0; i < 8; ++ i)
for (int Nx = X + Dir[i][0], Ny = Y + Dir[i][1]; 0 <= Nx && Nx < N && 0 <= Ny && Ny < M; Nx += Dir[i][0], Ny += Dir[i][1])
{
++ CX[Nx], ++ CY[Ny], ++ CLS[Nx + Ny], ++ CRS[Nx - Ny + M];
Search(X, Y + 1, Sum + 1);
-- CX[Nx], -- CY[Ny], -- CLS[Nx + Ny], -- CRS[Nx - Ny + M];
}
}
else Search(X, Y + 1, Sum);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("queen.in", "r", stdin);
freopen("queen.out", "w", stdout);
#endif
for (int Cases = 1; ; ++ Cases)
{
scanf("%d", &N);
if (!N) return 0;
scanf("%d\n", &M);
for (int i = 0; i < N; ++ i)
gets(Map[i]);
Ans = 5;
Search(0, 0, 0);
printf("Case %d: %d\n", Cases, Ans);
}
} 展开
using namespace std;
#define MaxN 10
const int Dir[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int N, M, Ans;
char Map[MaxN][MaxN];
int CX[MaxN], CY[MaxN], CLS[MaxN + MaxN], CRS[MaxN + MaxN];
void Search(int X, int Y, int Sum)
{
if (Sum >= Ans) return;
if (X == N)
{
Ans = Sum;
return;
}
if (Y == M)
{
Search(X + 1, 0, Sum);
return;
}
if (Map[X][Y] == 'X' && !CX[X] && !CY[Y] && !CLS[X + Y] && !CRS[X - Y + M])
{
++ CX[X], ++ CY[Y], ++ CLS[X + Y], ++ CRS[X - Y + M];
Search(X + 1, 0, Sum + 1);
-- CX[X], -- CY[Y], -- CLS[X + Y], -- CRS[X - Y + M];
for (int i = 0; i < 8; ++ i)
for (int Nx = X + Dir[i][0], Ny = Y + Dir[i][1]; 0 <= Nx && Nx < N && 0 <= Ny && Ny < M; Nx += Dir[i][0], Ny += Dir[i][1])
{
++ CX[Nx], ++ CY[Ny], ++ CLS[Nx + Ny], ++ CRS[Nx - Ny + M];
Search(X, Y + 1, Sum + 1);
-- CX[Nx], -- CY[Ny], -- CLS[Nx + Ny], -- CRS[Nx - Ny + M];
}
}
else Search(X, Y + 1, Sum);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("queen.in", "r", stdin);
freopen("queen.out", "w", stdout);
#endif
for (int Cases = 1; ; ++ Cases)
{
scanf("%d", &N);
if (!N) return 0;
scanf("%d\n", &M);
for (int i = 0; i < N; ++ i)
gets(Map[i]);
Ans = 5;
Search(0, 0, 0);
printf("Case %d: %d\n", Cases, Ans);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询