using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _5
{
class Manchine
{
public int[,] Mach(int n,int m)
{
//队列中0 表示没有机器人看守 1表示有 7 表示机器人位置 -1 表示死路
int[,] hou=new int[n,m];
int index = 0;
int index2 = 0;
//int Minbj = 0; //判断是否为边角
//int Maxbj = 0; //判断是否为边角
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
hou[i,j] = 0;
}
}
hou[0, 1] = 7;
hou[0, 0] = 1;
hou[0, 2] = 1;
hou[1, 1] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (hou[i, j] == 0)
{
if (Check(hou, i, j, n, m) == true)
{
hou[i, j] = 7;
hou=MCheck(hou, i, j, n, m);
Console.WriteLine("成功 第 " + index++ + " 种");
}
}
}
}
return hou;
}
public int[,] Banjiao(int[,] h, int x, int y, int n, int m)
{
if (x == 0 && (y > 0 && (y<=m-1)))
{
h[x + 1, y] = 1;
if (y < m - 1) h[x, y+1] = 1;
}
else if (x == n - 1 && (y >= 0 && (y <= m - 1)))
{
if (h[x - 1, y]!=7) h[x - 1, y] = 1;
if(y==m-1) h[x , y-1] = 1;
if (h[x, y] == 7)
{
if (y>0 && y <= m - 1)
{
if (x == n - 1 && y == m - 1)
{
}
else
{
h[x, y - 1] = 1;
h[x, y + 1] = 1;
}
}
}
}
else if (y == 0 && (x > 0 && (x <= n - 1)))
{
h[x, y+1] = 1;
if (x > 0) h[x-1, y] = 1;
if (x <= n-1) h[x +1, y] = 1;
}
else if (y == n - 1 && (x >= 0 && (x <= n - 1)))
{
h[x, y-1] = 1;
if(x==n-1) h[x - 1, y] = 1;
if (h[x, y] == 7)
{
if (x > 0 && y <= m - 1)
{
h[x - 1, y] = 1;
}
}
}
return h;
}
public int[,] MCheck(int[,] h,int x,int y,int n,int m)
{
if(h[x, y] == 7)
{
if ((x > 0 && x < (n - 1)) && (y > 0 && y < (m - 1)))
{
h[x, y - 1] = 1;
h[x, y + 1] = 1;
h[x - 1, y] = 1;
h[x + 1, y] = 1;
}
else if (x == 0 || y == 0 || x == n - 1 || y == m - 1)
{
h=Banjiao(h, x ,y ,n, m);
}
}
return h;
}
public bool Banjiao2(int[,] h, int x, int y, int n, int m)
{
if (x == 0 && (y > 0 && (y <= m - 1)))
{
if (h[x + 1, y] == 1 && h[x, y - 1] == 1)
return false;
else
return true;
}
else if (x == n - 1 && (y >= 0 && (y <= m - 1)))
{
if (h[x - 1, y] == 1)
{
if (x > 0 && y>0)
{
if (y == m - 1)
{
if(h[x - 1, y] == 1 && h[x, y - 1] == 1)
{
return true;
}
}
else
{
if (h[x - 1, y] == 1 && h[x, y - 1] == 1 && h[x, y + 1] == 1)
{
return true;
}
}
}
return false;
}
else if (y == m - 1 && h[x, y - 1] == 1)
return false;
else
return true;
}
else if (y == 0 && (x > 0 && (x <= n - 1)))
{
if (h[x, y + 1] == 1 && h[x-1, y] == 1)
return false;
else
return true;
}
else if (y == n - 1 && (x >= 0 && (x <= n - 1)))
{
if (h[x, y - 1] == 1)
{
if (x > 0 && y > 0)
{
if (y == m - 1)
{
if (h[x - 1, y] == 0 && h[x, y - 1] == 1)
{
return true;
}
}
}
return false;
}
else if (x == n - 1 && h[x - 1, y] == 1)
return false;
else
return true;
}
return true;
}
public bool Check(int[,] h,int x,int y,int n,int m)
{
if ((x > 0 && x < (n - 1)) && (y > 0 && y < (m - 1)))
{
if (h[x, y - 1] == 0 || h[x, y + 1] == 0 || h[x, y + 1] == 0 || h[x, y + 1] == 0)
return true;
else
return false;
}
else if (x == 0 || y == 0 || x == n - 1 || y == m - 1)
{
return Banjiao2(h, x, y, n, m);
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入 0 退出");
int c = 0;
c = Convert.ToInt32(Console.ReadLine());
while (true)
{
if (c == 0) break;
int n, m;
Console.WriteLine("请输入行数:N>=3");
n = Convert.ToInt32(Console.ReadLine());
while (true)
{
if (n >= 3) break;
Console.WriteLine("请重新输入行数:N>=3");
n = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("请输入列数:M>=3");
m = Convert.ToInt32(Console.ReadLine());
while (true)
{
if (m >= 3) break;
Console.WriteLine("请重新输入行数列数:M>=3");
m = Convert.ToInt32(Console.ReadLine());
}
int[,] fangjian = new int[n, m];
Manchine mc = new Manchine();
fangjian = mc.Mach(n, m);
int num = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (fangjian[i, j] == 7) num++;
Console.Write(fangjian[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("机器人数量为 " + num + " 个");
Console.WriteLine("输入 0 退出");
c = Convert.ToInt32(Console.ReadLine());
}
}
}
}