USACO Wisconsin Squares 解题报告

USER: chen chen [thestor1]
TASK: wissqu
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [2.206 secs, 3504 KB]

All tests OK.

YOUR PROGRAM ('wissqu') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

这道题题目理解起来比较困难,不过做起来很简单,就是个简单的DFS。没有用任何优化。而且测试样例到目前为止都只有一个。我不知道为什么第一个要是D,不过既然这么说了,就这样写了。我自己机子上面用了6秒,usaco的机器上面只用了2.2秒。

/* 
ID: thestor1 
LANG: C++ 
TASK: wissqu 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <climits>
#include <cassert>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

class Move
{
public:
	char ch;
	int r, c;
	Move(char ch, int r, int c) : ch(ch), r(r), c(c) {}
};

void DFS(int &cnt, std::vector<string> &square, std::vector<int> &target, int replaced, std::vector<Move> &moves);

void printSquare(std::vector<string> &square)
{
	cout << "[debug]square:" << endl;
	for (int i = 0; i < square.size(); ++i)
	{
		for (int j = 0; j < square[i].size(); ++j)
		{
			cout << square[i][j];
		}
		cout << endl;
	}
}

void fill(int i, int &cnt, std::vector<string> &square, std::vector<int> &target, int replaced, std::vector<Move> &moves)
{
	// cout << "[debug]cnt: " << cnt << ", replaced: " << replaced << endl;
	// printSquare(square);

	target[i]--;

	int nr, nc;
	bool conflict;
	char ch = 'A' + i;
	for (int r = 0; r < square.size(); ++r)
	{
		for (int c = 0; c < square[r].size(); ++c)
		{
			if (square[r][c] == ch || square[r][c] > 'E')
			{
				continue;
			}

			conflict = false;
			for (int d = 0; d < 8; ++d)
			{
				nr = r + dx[d], nc = c + dy[d];
				if (nr < 0 || nr > 3 || nc < 0 || nc > 3)
				{
					continue;
				}
				if (square[nr][nc] == ch || square[nr][nc] == ch - 'A' + 'a')
				{
					conflict = true;
					break;
				}
			}

			if (conflict)
			{
				continue;
			}

			// cout << "[debug]square[" << r << "][" << c << "]:" << square[r][c] << endl;
			char oldchar = square[r][c];
			square[r][c] = ch - 'A' + 'a';

			if (cnt == 0)
			{
				moves.push_back(Move(ch, r + 1, c + 1));
			}
			DFS(cnt, square, target, replaced + 1, moves);
			if (cnt == 0)
			{
				moves.pop_back();
			}
			square[r][c] = oldchar;
		}
	}

	target[i]++;
}

void DFS(int &cnt, std::vector<string> &square, std::vector<int> &target, int replaced, std::vector<Move> &moves)
{
	if (replaced == 16)
	{
		// cout << "[debug]target:" << endl;
		// for (int i = 0; i < target.size(); ++i)
		// {
			// cout << i << ":" << target[i] << endl;
		// }

		// printSquare(square);
		if (cnt == 0)
		{
			ofstream fout("wissqu.out");
			for (int i = 0; i < moves.size(); ++i)
			{
				fout << moves[i].ch << " " << moves[i].r << " " << moves[i].c << endl;
			}
			fout.close();
		}

		cnt++;
		// cout << "[debug]cnt: " << cnt << endl;
		return;
	}

	if (replaced == 0)
	{
		fill(3, cnt, square, target, replaced, moves);
	}
	else
	{
		for (int i = 0; i < target.size(); ++i)
		{
			if (target[i] == 0)
			{
				continue;
			}

			fill(i, cnt, square, target, replaced, moves);
		}
	}
}

int main()
{
	std::vector<string> square;
	ifstream fin("wissqu.in");

	string row;
	for (int r = 0; r < 4; ++r)
	{
		fin >> row;
		square.push_back(row);
	}
	
	fin.close();

	std::vector<int> target(5, 3);
	target[3] = 4;

	int cnt = 0;
	std::vector<Move> moves;
	DFS(cnt, square, target, 0, moves);

	ofstream fout("wissqu.out", std::ofstream::out | std::ofstream::app);
	fout << cnt << endl;
	fout.close();
	return 0;  
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值